js文件路径问题YII2

时间:2017-01-12 07:49:39

标签: yii yii2 yii2-advanced-app

我想要包含C:\xampp\htdocs\yii2\vendor\bower\backend\assets\js中的自定义js文件,但是控制台会给我错误

  

无法加载资源:服务器响应状态为404(未找到)

虽然其他相同的文件具有相同的上述目录,但正在工作 我的appAsset文件位于C:\xampp\htdocs\yii2\backend\assets

<?php

namespace backend\assets;

use yii\web\AssetBundle;

/**
 * Main backend application asset bundle.
 */
class AppAsset extends AssetBundle
{
    //public $basePath = '@webroot';
    //public $baseUrl = '@web';
    public $sourcePath = '@bower/backend/';
    public $css = [
            'assets/css/chosen.css',
            'assets/css/style.css',
            'assets/css/font-awesome.min.css',
            'assets/css/bootstrap.min.css',
            //'assets/css/bootstrap.css',
            'assets/css/jquery.dataTables.min.css',
            'assets/css/w3.css',
            'assets/css/jquery-ui.css', 
    ];
    public $js = [
        //'assets/js/jquery.min.js',
        'assets/js/jquery-ui.js',
        'assets/js/jquery.dataTables.min.js',
        'assets/js/jquery-ui.multidatespicker.js',
        'assets/js/chosen.jquery.js',
        'assets/js/chosen.jquery.js',
        'assets/js/my-custom.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

和my-custom.js

<script>
    $(document).ready(function(){
       $('li.active .treeview').on('click', function(e) {

          $('li.active .treeview-menu').toggleClass("hide");
          e.preventDefault();
        });
    });
</script>

未找到的文件的网址是

http://localhost/yii2/backend/web/assets/c4875c89/assets/js/my-custom.js

enter image description here

2 个答案:

答案 0 :(得分:1)

只需在视图文件中添加

即可
<?php 
   $this->registerJsFile('PATH_TO_FILE');
?>

答案 1 :(得分:1)

使用RegisterJsFile概念:

您只需在视图中注册此js文件,例如:

$this->registerJsFile('@web/js/specific.js');

或你的委托

 $this->registerJsFile('PATH_TO_FILELOCATION');

了解详情:http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html#registering-scripts

http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail