我正在使用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();
答案 0 :(得分:2)
您的功能不再“连接”到浏览器窗口,处理程序已更改。
这意味着我们必须使“窗口”对象知道您的新功能。
window.newInvLine = function() {
alert("Hi");
};
答案 1 :(得分:1)
还有一个global
常量可以使用。它将使该功能全局可用。示例:
const dt = require('datatables.net-responsive-bs');
global.dt = dt;