在 Symfony 4 中使用引导程序和 Webpack Encore [英] Using bootstrap with Webpack Encore in Symfony 4
问题描述
我想在 Symfony 4.1 中将引导程序与 Webpack Encore 一起使用,但引导程序不起作用.在这篇文章的 template/base.html.twig 文件中,我使用了一些引导程序类,但没有考虑到,我不明白为什么.
I would like to use bootstrap with Webpack Encore in Symfony 4.1 but bootstrap does not function. In the template/base.html.twig file in this post, I used some bootstrap classes but it is not taken into account and I don't understand why.
我用纱线安装了引导程序所需的依赖项:
I installed the dependencies I need for bootstrap with yarn:
yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev
template/base.html.twig
在这个文件中,我使用了资产函数来考虑以下文件:build/app.scss 和 build/app.js
In this file, I used the asset function in order to take into account the files: build/app.scss and build/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>
在以下两个文件中,我需要并导入了引导程序所需的内容.
In the two following files, I required and imported what I need for bootstrap.
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 作为全局变量.
In this file, I used enableSassLoader() in order to activate Sass and autoProvidejQuery() in order to have access to jQuery as a global variable.
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 可以正确构建所有内容.但是,我在屏幕上看不到引导程序主题.
The command yarn encore dev builds everything correctly. But, I don't see the bootstrap theme in the screen.
提前致谢,
推荐答案
head 中的 template/base.html.twig 文件中存在错误.public/build 目录中全局 asset/js/app.scss 的结果是一个 app.css 文件,而不是 .scss 扩展文件.它是我们可以在 assets/js/目录中找到的所有 javascript 文件的构建结果.
There is an error in the template/base.html.twig file in the head. The result of the global asset/js/app.scss in the public/build directory is a app.css file and not a .scss extension file. It is the result of the build of all the javascript files we can find in the assets/js/ directory.
<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>
这篇关于在 Symfony 4 中使用引导程序和 Webpack Encore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!