gpt4 book ai didi

javascript - SF4/WWebpack - 未定义 Javascript 函数

转载 作者:行者123 更新时间:2023-11-29 18:47:54 24 4
gpt4 key购买 nike

我正在使用 Symfony 的 WebPack.Encore 包,我意识到一件事是对于单个按钮 onclick = function (),我的函数没有定义,而如果我把它放在 document.ready 中,它就可以工作。你能给我解释一下如何在 onclick 上调用这个函数吗?

// file.js
function newInvLine(){
alert("Hi");
}

// html
<a href="#" onclick="newInvLine();">TEST</a>

我的 webpack.config

var Encore = require('@symfony/webpack-encore');

Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/public/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')

/*
* ENTRY CONFIG
*
* Add 1 entry for each "page" of your app
* (including one that's included on every page - e.g. "app")
*
* Each entry will result in one JavaScript file (e.g. app.js)
* and one CSS file (e.g. app.css) if you JavaScript imports CSS.
*/
.addEntry('app', './assets/js/app.js')
.addEntry('find_city_by_cp', './assets/js/find_city_by_cp.js')
.addEntry('calculSimulateur', './assets/js/file.js')
//.addEntry('page1', './assets/js/page1.js')
//.addEntry('page2', './assets/js/page2.js')

/*
* FEATURE CONFIG
*
* Enable & configure other features below. For a full
* list of features, see:
* https://symfony.com/doc/current/frontend.html#adding-more-features
*/
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())

// enables Sass/SCSS support
.enableSassLoader()

// uncomment if you use TypeScript
//.enableTypeScriptLoader()

// uncomment if you're having problems with a jQuery plugin
.autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

最佳答案

您的函数不再“连接”到您的浏览器窗口,处理程序已更改。

这意味着我们必须让“窗口”对象知道您的新功能。

window.newInvLine = function() {
alert("Hi");
};

关于javascript - SF4/WWebpack - 未定义 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52556362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com