YII2 使用js

时间:2021-10-29 07:13:21

1、在 /backend/assets/ 中新建一个文件 CollectionAsset.php

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/ namespace backend\assets; use yii\web\AssetBundle; /**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CollectionAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/collection.js'//需要引入的js文件
];
public $depends = [
'yii\web\JqueryAsset',//依赖
];
}

2、执行引入

CollectionAsset::register($this->view);

3、在js脚本中写入程序