简体   繁体   English

Symfony 5 / Encore - Javascript 问题

[英]Symfony 5 / Encore - Javascript issue

I'm a beginner with Symfony and I just finished my first website.我是 Symfony 的初学者,我刚刚完成了我的第一个网站。 The website I built is based on Bootstrap and I'm using some jquery for additional js files I made.我构建的网站基于 Bootstrap,我正在使用一些 jquery 来创建我制作的其他 js 文件。

I'm having some trouble with Webpack and Encore as neither my Bootstrap JS or jQuery are working.我在使用 Webpack 和 Encore 时遇到了一些麻烦,因为我的 Bootstrap JS 或 jQuery 都不起作用。 I'm having no problem with my SCSS and my homemade js are working (except that they don't get jQuery).我的 SCSS 没有问题,我自制的 js 正在运行(除了它们没有 jQuery)。

What I did :我做了什么 :

npm install --save-dev jquery
npm install --save-dev popper.js
npm install --save-dev bootstrap

In my webpack.config.js (Encore) :在我的webpack.config.js (Encore) 中

.setOutputPath('public/build/')
.setPublicPath('/build')

.addEntry('js/ad', './assets/js/ad.js')
.addStyleEntry('css/app', './assets/css/app.scss')

.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabelPresetEnv((config) => {
    config.useBuiltIns = 'usage';
    config.corejs = 3;
})
.enableSassLoader()

I tried also with and without我也试过有和没有

.autoProvidejQuery()

In my app.js在我的app.js 中

import $ from 'jquery';

global.$ = global.jQuery = $;

import 'bootstrap';

In my base.html.twig在我的base.html.twig

<script src="{{ asset('build/js/app.js') }}"></script>

When everything is fine, I'm finally running in the console当一切正常时,我终于在控制台中运行

npm run dev

or或者

npm run build

After that, my js features are not working.之后,我的 js 功能不起作用。 On the pages requiring jQuery, I'm having an error in the console.在需要 jQuery 的页面上,我在控制台中遇到错误。

Uncaught ReferenceError: jQuery is not defined
    at bootstrap-datepicker.min.js:7
    at bootstrap-datepicker.min.js:7
book:94 Uncaught ReferenceError: $ is not defined
    at book:94

I'm a bit lost and I don't know what to try.我有点迷茫,我不知道该尝试什么。 If I manually add my js files without using Encore, everything works fine.如果我在不使用 Encore 的情况下手动添加我的 js 文件,则一切正常。

Thanks in advance for helping !提前感谢您的帮助!

with the update encore you need to import the css and the jquery in your js like this使用更新 encore,您需要像这样在 js 中导入 css 和 jquery

//your css files
import '../css/yourCss.css';
//jquery just the $ sign 
const $ = require('jquery');

then you need to make one entry in your webpack config file like那么你需要在你的 webpack 配置文件中创建一个条目,比如

 //you have to make it one word like app, ad 

.addEntry('ad', './assets/js/ad.js')

for using the js and css in your twig files you need to add the css with this builtin function of encore without the normal link tag just this line要在您的树枝文件中使用 js 和 css,您需要使用 encore 的这个内置函数添加 css,而没有普通的链接标签,就在这一行

{{ encore_entry_link_tags('ad') }} // ad = the entry name

for using java script you need to use this function要使用 java 脚本,您需要使用此功能

{{ encore_entry_script_tags('ad') }} // ad = the entry name

if you are using vs code i recommende to use this extension https://marketplace.visualstudio.com/items?itemName=nadim-vscode.symfony-extension-pack如果您使用 vs 代码,我建议您使用此扩展程序https://marketplace.visualstudio.com/items?itemName=nadim-vscode.symfony-extension-pack

with this extension you type just encorecss in your twig file and the extension will auto complete the function for you使用此扩展程序,您只需在树枝文件中键入 encorecss,该扩展程序将自动为您完成该功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM