简体   繁体   English

Laravel 与引导程序 4 混合

[英]Laravel Mix with Bootstrap 4

I have been learning Laravel (8) and have enjoyed working with tailwindcss.我一直在学习 Laravel (8) 并且喜欢使用 tailwindcss。 That said there are some things I still wish to use Bootstrap for.也就是说,有些事情我仍然希望使用 Bootstrap。 I am having trouble locating documentation on how to set up bootstrap with laravel mix in laravel 8. More specifically, in the resources/css/app.css file we put the following for tailwind:我无法找到有关如何使用 laravel 8 中的 laravel 混合设置引导程序的文档。更具体地说,在resources/css/app.css文件添加了以下内容:

@tailwind base;
@tailwind components;
@tailwind utilities;

but Im not sure what would go here for bootstrap.但我不确定 go 在这里引导什么。

I noticed older versions of laravel used php artisan ui bootstrap but that is not available in Laravel 8 from what I have seen.我注意到旧版本的 laravel 使用php artisan ui bootstrap ,但据我所见,这在 Laravel 8 中不可用。

Run: npm install --save-dev bootstrap jquery popper.js运行: npm install --save-dev bootstrap jquery popper.js

How your webpack.mix.js should look:您的webpack.mix.js应该是什么样子:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
  .sass('resources/sass/app.scss', 'public/css');

How your app.scss should look:你的app.scss应该是什么样子:

@import '~bootstrap/scss/bootstrap';

How your app.js should look:你的app.js应该是怎样的:

window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js');
require('bootstrap');

This is how you could use generated files in your .blade.php files:这是您可以在.blade.php文件中使用生成文件的方式:

<link rel="stylesheet" href="{{ mix('css/app.css') }}" type="text/css">
<script src="{{ mix('js/app.js') }}"></script>

If you want to customize Bootstrap, copy node_modules/bootstrap/scss/_variables.scss to resources/sass/_variables.scss , change variables you want and change your app.scss to:如果要自定义 Bootstrap, node_modules/bootstrap/scss/_variables.scssresources/sass/_variables.scss ,更改所需的变量并将app.scss更改为:

@import '~bootstrap/scss/functions';
@import 'variables';
@import '~bootstrap/scss/bootstrap'; 

You have some helpful documentation on https://getbootstrap.com/docs/4.0/getting-started/webpack/您有一些关于https 的有用文档://getbootstrap.com/docs/4.0/getting-started/webpack/

Laracasts is also quite helpful. Laracasts 也很有帮助。

Seems to be available in Laravel v8.24.0 though.不过似乎在 Laravel v8.24.0 中可用。

Be aware that doing this procedure do regenerate the webpack.mix.js.请注意,执行此过程会重新生成 webpack.mix.js。

Steps are:步骤是:

  • install laravel ui: composer require laravel/ui安装 laravel ui: composer require laravel/ui
  • generate scaffolding: php artisan ui bootstrap生成脚手架: php artisan ui bootstrap
  • optionnaly generate the same for auth: php artisan ui bootstrap --auth可选地为身份验证生成相同的: php artisan ui bootstrap --auth
  • make npm install required packages: npm install使 npm 安装所需的包: npm install
  • compile assets: npm run dev or npm run production (for development or for production)编译资产: npm run devnpm run production (用于开发或生产)

Then make sure to have the following in your blade templates:然后确保在刀片模板中包含以下内容:

<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>

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

use this steps使用此步骤

npm install bootstrap
npm install @popperjs/core
npm install sass@^1.32
npm install sass-loader

For Bootstrap Sass files, let's create the “scss” folder in /resources and then a new app.scss file in /resources/scss/.对于 Bootstrap Sass 文件,让我们在 /resources 中创建“scss”文件夹,然后在 /resources/scss/ 中创建一个新的 app.scss 文件。 Then insert the following line to the /ressources/scss/app.scss file然后将以下行插入 /ressources/scss/app.scss 文件

@import "bootstrap";

For Bootstrap JavaScript, insert the following line to the /resources/js/app.js file:对于 Bootstrap JavaScript,将以下行插入 /resources/js/app.js 文件:

import "bootstrap";

To compile Bootstrap JavaScript and Sass files with Laravel Mix, you need to edit the /webpack.mix.js file as follows:用 Laravel Mix 编译 Bootstrap JavaScript 和 Sass 文件,你需要编辑 /webpack.mix.js 文件如下:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
    .sass("resources/scss/app.scss", "public/css");

now you can run you command现在你可以运行你的命令

npm run dev

Now that we have the /public/css/app.css and /public/js/app.js files with Bootstrap included, we can include them on a page (template Blade) via Laravel's asset helper () to use the Bootstrap components现在我们有了包含 Bootstrap 的 /public/css/app.css 和 /public/js/app.js 文件,我们可以通过 Laravel 的资产助手 () 将它们包含在页面(模板 Blade)中以使用 Bootstrap 组件

On your Head在你的头上

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

On your footer在你的页脚

<script src="{{ asset('js/app.js') }}"></script>

now i think he will work enjoy现在我认为他会享受工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM