Yii Bootstrap not loading JS files

别说谁变了你拦得住时间么 提交于 2019-12-23 03:49:08

问题


I'm using Yii-Bootstrap in my brand new Yii project. I followed the install instructions exactly.

On the front page I am using the JS Carousel plugin.

$this->widget('bootstrap.widgets.TbCarousel', array(
    'items'=>array(
        array(
            'image'=>'http://placehold.it/770x400&text=First+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
        array(
            'image'=>'http://placehold.it/770x400&text=Second+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
        array(
            'image'=>'http://placehold.it/770x400&text=Third+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
    ),
));

Uncaught TypeError: Object [object Object] has no method 'carousel'

This leads to a .carousel() not found error. It's like the JS files are not being registered. The CSS and everything looks fine.

I'm using the yii-environment extension..

return array(

    // Set yiiPath (relative to Environment.php)
    'yiiPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yii.php',
    'yiicPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yiic.php',
    'yiitPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yiit.php',

    // Static function Yii::setPathOfAlias()
    'yiiSetPathOfAlias' => array(
        // uncomment the following to define a path alias
        'bootstrap' => realpath(dirname(__FILE__).'/../extensions/bootstrap')
    ),

    // This is the main Web application configuration. Any writable
    // CWebApplication properties can be configured here.
    'configWeb' => array(

        'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
            'theme' => 'bootstrap',

        // Preloading 'log' component
        'preload' => array('log'),

        // Autoloading model and component classes
        'import' => array(
            'application.models.*',
            'application.components.*',
        ),

        // Application components
        'components' => array(
            'bootstrap'=>array(
                'class'=>'bootstrap.components.Bootstrap',
            ),

Any suggestions?


回答1:


You will have to register the css and js specifically:

Yii::app()->bootstrap->register();

which is different from the way it was done in older versions of the extension. I do not know why this is not mentioned in the documentation anywhere.

A good place to put the above line of code is in your layout, if your entire site/app uses yii-bootstrap, or if you are doing it in only specific views, you can put it in those views.

P.S - You can find the function in the bootstrap/components/Bootstrap.php file.




回答2:


<?php Yii::app()->bootstrap->registerAllCss(); ?>

<?php Yii::app()->bootstrap->registerCoreScripts(); ?>
<?php Yii::app()->clientScript->registerCssFile(Yii::app()->request->baseUrl.'/css/styles.css'); ?>


来源:https://stackoverflow.com/questions/15127400/yii-bootstrap-not-loading-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!