Symfony 4/Webpack Encore:jQuery不起作用 [英] Symfony 4 / Webpack Encore : jQuery doesn't work

查看:111
本文介绍了Symfony 4/Webpack Encore:jQuery不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery不起作用,我无法使用引导程序下拉列表,自定义javascript ...

jQuery doesn't work and I can't use my bootstrap dropdown, custom javascript...

npm run dev(或npm run build)之后,会很好地创建app.js文件并将其加载到浏览器中.

After a npm run dev (or npm run build), app.js file is well created and loads in the browser.

编译完成,没有任何错误.

Compilation is done without any error.

我尝试先启用.autoProvidejQuery(),然后再启用npm run dev/npm run build,但没有任何变化.

I tried to enable .autoProvidejQuery() then npm run dev / npm run build, but nothing changes.

我正在使用Symfony 4.1.6

I am using Symfony 4.1.6

找到解决方案

webpack.config.js

如果仅在行上注释,它可以工作,但是在运行npm run devnpm run build时会出现警告消息.

If you just comment on the line, it works but you have a warning message when you run a npm run dev or npm 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-未定义$

推荐答案

找到解决方案

webpack.config.js

如果仅在行上注释,它可以工作,但是在运行npm run devnpm run build时会出现警告消息.

If you just comment on the line, it works but you have a warning message when you run a npm run dev or npm run build.

这篇关于Symfony 4/Webpack Encore:jQuery不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆