Yii2 BootstrapAsset未加载bootstrap.js

时间:2015-05-19 13:57:12

标签: javascript twitter-bootstrap-3 yii2

yii\bootstrap\BootstrapAsset未加载bootstrap.js,“modal”等元素无效。

class AppAsset extends AssetBundle {
    public $basePath = '@webroot';

    public $baseUrl = '@web';

    public $css = [    
        /* theme */
        'css/site.css',

        /* jasny bootstrap */
        'public/jasny/css/jasny-bootstrap.min.css',

        /* font awesome */
        'public/font-awesome-430/css/font-awesome.min.css',

        /* font roboto */
        'public/fonts/roboto/roboto.css',

        /* Data Tables */
        'public/datatables/extensions/integration/bootstrap/3/dataTables.bootstrap.css',  
    ];

    public $js = [    
        /* jasny bootstrap  */
        'public/jasny/js/jasny-bootstrap.min.js',

        /* Data Tables  */
        'public/datatables/datajs.js',
        'public/datatables/media/js/jquery.dataTables.min.js',
        'public/datatables/extensions/integration/bootstrap/3/dataTables.bootstrap.js',               
    ];

    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];    
}

Here是图书馆的屏幕,bootstrap.js在那里缺失。

5 个答案:

答案 0 :(得分:28)

如果您查看BootstrapAsset捆绑包的内容,您会发现没有bootstrap.js

class BootstrapAsset extends AssetBundle
{
    public $sourcePath = '@bower/bootstrap/dist';
    public $css = [
        'css/bootstrap.css',
    ];
}

对于bootstrap.js,存在另一个资产包,它名为BootstrapPluginAsset

class BootstrapPluginAsset extends AssetBundle
{
    public $sourcePath = '@bower/bootstrap/dist';
    public $js = [
        'js/bootstrap.js',
    ];
    public $depends = [
        'yii\web\JqueryAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

带有命名空间的完整类名是:yii\bootstrap\BootstrapPluginAsset

当您使用js依赖引导小部件(例如yii\bootstrap\Modal等)时会自动包含它。

答案 1 :(得分:18)

你可以尝试一下:

class AppAsset extends AssetBundle{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
    'css/mystyle.css',
];
public $js = [

];
public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapPluginAsset',
];
}

答案 2 :(得分:7)

添加依赖项

 public $depends = [
        'yii\bootstrap\BootstrapAsset',
    ];

它只添加bootstrap css

添加

 public $depends = [
     'yii\bootstrap\BootstrapPluginAsset',
    ];

它添加bootstrap css以及js

答案 3 :(得分:1)

您可以在主配置文件(config/web.php)中执行此操作:

'components' => [
    'assetManager' => [
        'bundles' => [
            'yii\bootstrap\BootstrapAsset' => [
                'js' => ['js/bootstrap.js'],
            ],
        ],
    ],
],

答案 4 :(得分:0)

您可以在AppAsset.php中尝试此操作

class AppAsset extends AssetBundle{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
  'css/mystyle.css',
];
public $js = [

];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapPluginAsset',
];
}

并且还会更改config / main.php

'components' => [
'assetManager' => [
    'bundles' => [
        'yii\bootstrap\BootstrapAsset' => [
            'js' => ['js/bootstrap.min.js'],
        ],
    ],
],
],

我已经尝试过这些可以解决我的问题的步骤