yii2 registering JS files to a View

前端 未结 5 1339
慢半拍i
慢半拍i 2021-02-05 07:34

I have A.php view file in /views/A/ folder. And I have A.js js file in /views/A/ folder Please help me register js file in view file.

As I unde

5条回答
  •  半阙折子戏
    2021-02-05 08:03

    If you register a js file by:

    $this->registerJsFile("@web/js/all.js");
    

    This will work but you will not be able to use jQuery. Because this file all.js is loaded before jQuery. To load after jQuery we make it depend it on 'yii\web\YiiAsset' or on \yii\web\JqueryAsset . So it will be loaded after jQuery.js. Example:

    $this->registerJsFile("@web/js/all.js",[
        'depends' => [
            \yii\web\JqueryAsset::className()
        ]
    ]);
    

    So What is difference between \yii\web\JqueryAsset and \yii\web\YiiAsset?

    In jQueryAsset the js file will load after jQuery.js and in YiiAsset the js file will load after yii.js file.

    If you want to create your own custom Asset Bundle:

提交回复
热议问题