Yii2 加载bootstrap.js

问题:加载了 frontend\assets\AppAsset 资源包,但是 bootstrap.js 文件没有被加载,打开 \vendor\yiisoft\yii2-bootstrap\src\BootstrapAsset.php,只加载了 css/bootstrap.css。

但是首页会自动载入bootstrap.js文件,后来发现因为在 layout/main.php 中用到了 NavBar(导航)组件,在导航中会依赖 bootstrap 的 js 包。
解决方法:使用 /vendor/yiisoft/yii2-Bootstrap/BootstrapPluginAsset.php

在\frontend\assets\AppAsset.php中,引入'yii\bootstrap\BootstrapPluginAsset',

<?php
namespace frontend\assets;
use yii\web\AssetBundle;

/**
 * Main frontend application asset bundle.
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',//bootstrap.css
        'yii\bootstrap\BootstrapPluginAsset',//bootstrap.js
    ];
}


参考:
https://qa.1r1g.com/sf/ask/2122944701

posted @ 2021-09-15 21:33  MeetU  阅读(146)  评论(0编辑  收藏  举报