javascript Yii2 BootstrapAsset 未加载 bootstrap.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30327781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:03:04  来源:igfitidea点击:

Yii2 BootstrapAsset is not loading bootstrap.js

javascripttwitter-bootstrap-3yii2

提问by ccdiego5

yii\bootstrap\BootstrapAssetis not loading bootstrap.js, and such elements as "modal" and others are not working.

yii\bootstrap\BootstrapAsset未加载bootstrap.js,并且诸如“模态”等元素不起作用。

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',
    ];    
}

Hereis screen of libraries, bootstrap.jsis missing there.

是图书馆的屏幕,bootstrap.js那里不见了。

采纳答案by mzk10k

You can try it:

你可以试试看:

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',
];
}

回答by arogachev

If you look at the content of BootstrapAssetbundle, you will see that there is no bootstrap.js:

如果您查看BootstrapAssetbundle的内容,您会看到没有bootstrap.js

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

For bootstrap.jsanother asset bundle exists and it's called BootstrapPluginAsset:

对于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',
    ];
}

The full class name with namespace is: yii\bootstrap\BootstrapPluginAsset.

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

It's included automatically when you use js dependent bootstrap widgets such as yii\bootstrap\Modal, etc.

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

回答by Kalpesh Desai

when you add dependency

当您添加依赖项时

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

its only add bootstrap css

它只添加引导 css

while you add

当你添加

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

its add bootstrap css as well as js

它添加引导 css 以及 js

回答by Roman Grinyov

You can do this in the main configuration file (config/web.php):

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

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

回答by Shamsi786

You can try this in AppAsset.php

你可以在 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',
];
}

and also change in you config/main.php

并且还更改了您的 config/main.php

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

I have tried these steps that will solve my problem thats to all for your comment

我已经尝试了这些步骤来解决我的问题,请大家发表评论