Yii2怎么引入css和js文件
1.首先需要在模板文件中添加
use frontend\assets\AppAsset;
AppAsset::register($this);
<?php $this--->beginPage() ?>
<?php $this--->beginBody() ?>
<?php $this--->endBody() ?>
<?php $this--->endPage() ?>
2.视图文件中可以通过以下方法注册
$this->registerJsFile('statics/libs/kindeditor/kindeditor-all-min.js',['position' => \yii\web\View::POS_HEAD]);
$this->registerJsFile('statics/tea/js/imagezoom.js',['position' => \yii\web\View::POS_END]);
$this->registerCssFile('statics/libs/kindeditor/themes/default/default.css');
$this->registerJs('
KindEditor.ready(function(K) {
window.editor = K.create("#post-content");
});
');
3.或者在AppAsset.php文件中进行配置
$this->registerJsFile('statics/libs/kindeditor/kindeditor-all-min.js',['position' => \yii\web\View::POS_HEAD]);
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'statics/css/bootstrap.min.css',
'statics/css/font-awesome.min.css',
'statics/css/ionicons.min.css',
'statics/css/AdminLTE.min.css',
'statics/css/skin-blue.min.css',
'statics/css/style.css',
];
public $js = [
'statics/js/bootstrap.min.js',
'statics/js/app.min.js',
'statics/js/tool.js',
];
public $depends = [
'yii\web\YiiAsset',
// 'yii\bootstrap\BootstrapAsset',
];
}