# Yii2引入Bootstrap.js

**URL:** <https://www.shiqidu.com/t/topic/350>\
**Category:** 开发调优\
**Tags:** Yii\
**Created:** [2017 年12 月 25 日 06:38 UTC](https://www.shiqidu.com/t/topic/350 "2017-12-25T06:38:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年12 月 25 日 06:38 UTC](https://www.shiqidu.com/t/topic/350/1 "2017-12-25T06:38:51Z")

</div>

我的是basic版，所以拿basic版举例。因为yii-bootstrap这个包是默认不引入bootstrap.js的，所以要想使用bootstrap.js，需要多写几行代码。

首先在`assets`目录下新建一个`BootstrapJsAsset.php`文件，内容如下。

```php
namespace app\assets;  
  
use yii\bootstrap\BootstrapAsset;  
  
class BootstrapJsAsset extends BootstrapAsset  
{  
    public $js = [  
        'js/bootstrap.js'  
    ];  
}  

```

然后在`AppAsset.php`中新加一个依赖。

```php
public $depends = [  
        'yii\web\YiiAsset',  
        'yii\bootstrap\BootstrapAsset',  
        'yii\bootstrap\PopperAsset',  
        'rmrevin\yii\fontawesome\AssetBundle',  
        'app\assets\BootstrapJsAsset'  
    ];  

```

* * *

更新，直接在 `AppAsset.php` 中的 `$depends` 里增加一行代码即可。

```php
'yii\bootstrap4\BootstrapPluginAsset'

```
