【问题标题】:Using bootstrap with Webpack Encore in Symfony 4在 Symfony 4 中使用带有 Webpack Encore 的引导程序
【发布时间】:2019-03-04 05:50:34
【问题描述】:

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

我使用 yarn 安装了引导程序所需的依赖项:

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

模板/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 命令可以正确构建所有内容。但是,我在屏幕上看不到引导主题。

提前致谢,

【问题讨论】:

    标签: bootstrap-4 symfony4 webpack-encore


    【解决方案1】:

    来自 WebpackEncoreBundle 的两个 Twig 助手可以为您完成大部分工作:

    https://symfony.com/doc/current/frontend/encore/simple-example.html#the-import-and-export-statements

    {# templates/base.html.twig #}
    <!DOCTYPE html>
    <html>
        <head>
            <!-- ... -->
    
            {% block stylesheets %}
                {# 'app' must match the first argument to addEntry() in webpack.config.js #}
                {{ encore_entry_link_tags('app') }}
    
                <!-- Renders a link tag (if your module requires any CSS)
                     <link rel="stylesheet" href="/build/app.css"> -->
            {% endblock %}
        </head>
        <body>
            <!-- ... -->
    
            {% block javascripts %}
                {{ encore_entry_script_tags('app') }}
    
                <!-- Renders app.js & a webpack runtime.js file
                    <script src="/build/runtime.js"></script>
                    <script src="/build/app.js"></script> -->
            {% endblock %}
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      head 中的 template/base.html.twig 文件有错误。 public/build 目录中的全局 asset/js/app.scss 的结果是一个 app.css 文件,而不是一个 .scss 扩展文件。这是我们可以在 assets/js/ 目录中找到的所有 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>
      

      【讨论】:

        猜你喜欢
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 2020-03-09
        • 1970-01-01
        • 2022-01-13
        • 2020-05-14
        • 2020-03-17
        • 2021-03-23
        相关资源
        最近更新 更多