最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
yii2.0模板引用js与css文件
时间:2022-06-25 00:50:30 编辑:袖梨 来源:一聚教程网
Yii2使用更加规范的方式,通过AppAsset::register($this)方法引入js和css文件,在Yii2的示例中,layouts的main.php中就有它的用法:AppAsset::register($this)。
在WEB目录中,会有一个assets目录,这个目录下有个Appasset.php文件,内容如下:
namespace frontendassets;
use yiiwebAssetBundle;
/**
* @author Qiang Xue
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yiiwebYiiAsset',
'yiibootstrapBootstrapAsset',
];
}
这个AppAsset类继承了YiiwebAssetBundle,它主要定义了js和css文件的路径和依赖。
在模版布局文件main.php使用AppAsset::register($this)注册这些css和js文件,除此之外,在html的head里面加上:
head() ?>
这句话是生成一个替换字符,表示css和js的引用代码在这里显示。别忘了在head里加上这句。
相关文章
- 庄园领主发展科技树怎么样 04-28
- 崩坏星穹铁道知更鸟机制分析 04-28
- 闪耀暖暖交织礼遇怎么玩 04-28
- 时空中的绘旅人罗夏CR疾奏怎么样 04-28
- 原神4.6版本阴间宝箱全收集攻略 04-28
- 光遇大耳狗茶话会怎么玩 04-28