Symfony 4 / Webpack Encore: jQuery不起作用

7

jQuery不能使用,我无法使用bootstrap下拉菜单、自定义JavaScript等。

在运行npm run devnpm run build之后,app.js文件已经成功创建并在浏览器中加载。

编译过程没有出现任何错误。

我尝试启用.autoProvidejQuery() 然后运行npm run dev / npm run build,但没有任何改变。

我正在使用Symfony 4.1.6.

解决方法

webpack.config.js中将.enableSingleRuntimeChunk()改为.disableSingleRuntimeChunk()

如果你只是注释掉这一行,它能够工作,但是当你运行npm run devnpm run build时会有一个警告信息。

package.json

"devDependencies": {
    "@symfony/webpack-encore": "^0.22.0",
    "bootstrap": "^4.2.1",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.6",
    "webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
    "dev-server": "encore dev-server",
    "dev": "encore dev",
    "watch": "encore dev --watch",
    "build": "encore production --progress"
},
"dependencies": {
    "node-sass": "^4.11.0",
    "sass-loader": "^7.1.0"
}

webpack.config.js

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('/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('js/app', './assets/js/app.js')
    .addEntry('js/ad', './assets/js/ad.js')
    .addStyleEntry('css/app', './assets/css/app.scss')
    //.addStyleEntry('css/bootstrap', './assets/css/bootstrap.min.css')

    // 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 Sass/SCSS support
    .enableSassLoader()

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

    // 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();

app.js

const $ = require('jquery');

global.$ = global.jQuery = $;

require('bootstrap');

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>SymBNB - {% block title %}Bienvenue !{% endblock %}</title>

        <meta name="viewport" content="width=device-width, user-scalable=no">

        <link rel="stylesheet" href="{{ asset('build/css/app.css') }}">

        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.0/css/all.css" integrity="sha384-aOkxzJ5uQz7WBObEZcHvV5JvRW3TUc2rNPA7pe3AwnsUohiw1Vj2Rgx2KSOkF5+h" crossorigin="anonymous">

        {# <link rel="stylesheet" href="/css/app.css"> #}
        {% block stylesheets %}{% endblock %}
    </head>
    <body>
        {% include 'partials/header.html.twig' %}

        {% block body %}{% endblock %}

        {% include 'partials/footer.html.twig' %}

        {#<script src="/js/jquery.min.js"></script>
        <script src="/js/popper.min.js"></script>
        <script src="/js/bootstrap.min.js"></script>#}
        <script src="{{ asset('build/js/app.js') }}"></script>
        {% block javascripts %}{% endblock %}
    </body>
</html>

类似话题:

Webpack Symfony Encore jQuery第三方插件

Webpack Encore - $未定义


你的 app.js 中的简单 console.log 是否显示结果了? - Bernard Pagoaga
没有,但我发现:它来自于webpack.config.js中的.enableSingleRuntimeChunk()。 - user9801655
你尝试将.addEntry('js/app', './assets/js/app.js')更改为.addEntry('app', './assets/js/app.js')了吗? - Bernard Pagoaga
第一条消息已经编辑,附上解决方案。 - user9801655
2个回答

6
如果你在你的webpack.config.js中使用了enableSingleRuntimeChunk(),那么你需要在你的基础模板中添加<script src="{{ asset('build/runtime.js') }}"></script>。有一个Twig助手encore_entry_script_tags()可以自动处理这个问题。

经过数小时不知道为什么我的应用程序无法工作,我找到了您的答案并且它起作用了。谢谢!! - Diguin

3

找到解决方案

webpack.config.js中将.enableSingleRuntimeChunk()改为.disableSingleRuntimeChunk()

如果您只是注释该行,则可以正常工作,但在运行npm run devnpm run build时会出现警告消息。


我仔细遵循了文档,并且在使用jQuery时没有遇到任何问题。 - Mike Doe
你使用的 webpack-encore 版本是多少? - user9801655
最新稳定版。 - Mike Doe
你看到我的文件和你的文件之间有什么不同吗? - user9801655

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接