首页 文章

在Symfony 4中使用Webpack Encore的bootstrap

提问于
浏览
1

我想在Symfony 4.1中使用Webpack Encore的bootstrap,但bootstrap不起作用 . 在这篇文章的template / base.html.twig文件中,我使用了一些引导类,但没有考虑到它,我不明白为什么 .

我使用yarn安装了bootstrap所需的依赖项:

yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev

template/base.html.twig

在此文件中,我使用资产功能以考虑文件: build/app.scssbuild/app.js

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}
        <link rel="stylesheet" href="{{ asset('build/app.scss') }}">
        {% endblock %}
    </head>
    <body>
        {% block body %}
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <a class="navbar-brand" href="#">Navbar</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" ar$
            <span class="navbar-toggler-icon"></span>
          </button>
        </nav>

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

在以下两个文件中,我需要并导入了引导程序所需的内容 .

assets/js/app.js

require('../css/app.scss');
var $ = require('jquery');
require('bootstrap');

assets/css/app.scss

@import "~bootstrap/scss/bootstrap";

webpack.config.js

在这个文件中,我使用了 enableSassLoader() 来激活Sass和 autoProvidejQuery() ,以便能够访问jQuery作为全局变量 .

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('app', './assets/js/app.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();

命令 yarn encore dev 正确构建所有内容 . 但是,我没有在屏幕上看到引导主题 .

提前致谢,

1 回答

  • 1

    头部的 template/base.html.twig 文件中存在错误 . public / build目录中全局 asset/js/app.scss 的结果是 app.css 文件,而不是.scss扩展名文件 . 这是我们可以在 assets/js/ directory. 中找到的所有javascript文件的构建结果

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}
        <link rel="stylesheet" href="{{ asset('build/app.css') }}">
        {% endblock %}
    </head>
    

相关问题