Yii2 BootstrapAsset is not loading bootstrap.js
17,641
Solution 1
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',
];
}
Solution 2
If you look at the content of BootstrapAsset
bundle, you will see that there is no bootstrap.js
:
class BootstrapAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap/dist';
public $css = [
'css/bootstrap.css',
];
}
For bootstrap.js
another asset bundle exists and it's called 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
.
It's included automatically when you use js dependent bootstrap widgets such as yii\bootstrap\Modal
, etc.
Solution 3
when you add dependency
public $depends = [
'yii\bootstrap\BootstrapAsset',
];
its only add bootstrap css
while you add
public $depends = [
'yii\bootstrap\BootstrapPluginAsset',
];
its add bootstrap css as well as js
Solution 4
You can do this in the main configuration file (config/web.php
):
'components' => [
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'js' => ['js/bootstrap.js'],
],
],
],
],
Author by
ccdiego5
Updated on June 27, 2022Comments
-
ccdiego5 almost 2 years
yii\bootstrap\BootstrapAsset
is not loadingbootstrap.js
, and such elements as "modal" and others are not working.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 is screen of libraries,
bootstrap.js
is missing there.