如何解决Symfony,Webpack,单击事件仅在产品环境中执行两次
我使用symfony/framework-bundle 5.0.5
和"symfony/webpack-encore-bundle": "dev-master"
当我在dev env中开发代码时,一切都是正确的,但是当我将项目切换到产品环境时,我遇到了奇怪的行为,我的on lick事件执行了两次,也许当我返回到dev env时,包括我不理解的js脚本的两倍问题不见了。如何在prod env的symfony框架中正确使用Web Pack?
webpack.config.js
var Encore = require('@symfony/webpack-encore');
// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
// directory where compiled assets will be stored
.setoutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/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 your JavaScript imports CSS.
*/
.addEntry('app','./assets/js/app.js')
.addEntry('index/js/index.min','./assets/js/index/index.js')
.addEntry('index/js/awin.min','./assets/js/index/awin.js')
.addEntry('index/js/adtraction.min','./assets/js/index/adtraction.js')
.addEntry('index/js/adrecord.min','./assets/js/index/adrecord.js')
.addEntry('index/js/TradeDoubler.min','./assets/js/index/TradeDoubler.js')
.addEntry('index/js/hover_menu.min','./assets/js/index/hover_menu.js')
.addEntry('index/js/brand_list.min','./assets/js/index/brand_list.js')
.addEntry('index/js/admin_shop_rule_list.min','./assets/js/index/admin_shop_rule_list.js')
.addEntry('index/js/resource_shop_list.min','./assets/js/index/resource_shop_list.js')
.addEntry('index/js/statisticsMonitoring.min','./assets/js/index/statisticsMonitoring.js')
//.addEntry('page1','./assets/js/page1.js')
//.addEntry('page2','./assets/js/page2.js')
// When enabled,Webpack "splits" your files into smaller pieces for greater optimization.
.splitEntryChunks()
// will require an extra script tag for runtime.js
// but,you probably want this,unless you're building a single-page app
.enableSingleRuntimeChunk()
/*
* 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 @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
// enables Sass/SCSS support
.enableSassLoader()
.addStyleEntry('index/css/index.min','./assets/css/app.scss')
// uncomment if you use TypeScript
//.enableTypeScriptLoader()
// uncomment to get integrity="..." attributes on your script & link tags
// requires WebpackEncoreBundle 1.4 or higher
//.enableIntegrityHashes(Encore.isProduction())
// uncomment if you're having problems with a jQuery plugin
.autoprovidejQuery()
// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin','./assets/js/admin.js')
;
module.exports = Encore.getWebpackConfig();
config / packages / prod / webpack_encore.yaml
webpack_encore:
# Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# Available in version 1.2
cache: true
config / packages / webpack_encore.yaml
webpack_encore:
# The path where Encore is building the assets - i.e. Encore.setoutputPath()
output_path: '%kernel.project_dir%/public/build'
我的基本树枝模板仅具有fosjsrouting
的脚本
{% block javascripts %}
<script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
<script src="{{ path('fos_js_routing_js',{ callback: 'fos.Router.setData' }) }}"></script>
{% endblock %}
以及我面临双击的子模板
{% extends 'base.html.twig' %}
//...///
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('index/js/resource_shop_list.min') }}
<script>
function getJSonObject(value) {
return $.parseJSON(value.replace(/"/ig,'"'));
}
let th_keys = getJSonObject("{{ dataTbaleKeys ? dataTbaleKeys|json_encode() : "{}" }}");
let for_prepare_defs = getJSonObject("{{ dataTbaleKeys ? dataTbaleKeys|json_encode() : "{}" }}");
</script>
{% endblock %}
和我的资产/js/index/resource_shop_list.js
/*
* Welcome to your app's main JavaScript file!
*
* We recommend including the built version of this JavaScript file
* (and its CSS file) in your base layout (base.html.twig).
*/
// any CSS you import will output into a single css file (app.css in this case)
import '../../css/app.scss';
import * as dt_bs4 from 'datatables.net-bs4'
import * as fh_bs from 'datatables.net-fixedheader-bs4';
import * as r_bs from 'datatables.net-responsive-bs4';
require('@fortawesome/fontawesome-free/css/all.min.css');
require('datatables.net-dt/css/jquery.dataTables.min.css');
require('datatables.net-fixedheader-bs4/css/fixedHeader.bootstrap4.min.css');
require('datatables.net-responsive-bs4/css/responsive.bootstrap4.min.css');
require('@fortawesome/fontawesome-free/js/all.js');
const $ = require('jquery');
global.$ = global.jQuery = $;
// import 'popper.js';
require('bootstrap');
document.addEventListener("DOMContentLoaded",function () {
console.log("resource shop list!");
const body = $('body');
const app_rest_admin_resourceshops_shopreloading = window.Routing.generate('app_rest_admin_resourceshops_shopreloading');
var table;
body.on('click','.resource_reloading button',function () {
let current = $(this);
console.log(current);
let shopName = current.data('shopName');
reloadingShop(shopName);
});
// ...//
我仔细检查了一次,我只创建了一次事件,在dev env中它是正确的。 对于服务器中的prod env,我执行了
PHP bin/console c:c && yarn encore production && chmod 777 -R
var/cache/ var/log/
有结果
Done in 13.51s.
如何解决?
解决方法
您可以将 jQuery 侦听器从 on()
更改为 one()
以防止重复事件:
body.one('click','.resource_reloading button',function () {
// ...
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。