There are several ways to introduce:
1, the view may be introduced directly on the page
<?php
use yii\helpers\Html;
?>
<?=Html::cssFile('@web/css/index.css')?>
<?=Html::jsFile('@web/js/jquery.min.js')?>
2, may be directly introduced into the native code write, project directory path / web / css or / js
<script src="js/nav.js"></script>
3, you can use assetBundle management style and css js script
resource package definition: basic / assets / AppAsset.php
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\assets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
'css/base.css'
];
public $js = [
'js/sliders.js'
];
public $depends = [ //依赖包,没有可以不写
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
//定义按需加载JS方法,注意加载顺序在最后
public static function addScript($view, $jsfile) {
$view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);
}
//定义按需加载css方法,注意加载顺序在最后
public static function addCss($view, $cssfile) {
$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);
}
}
At the beginning of the view files are written:
<?php
use yii\helpers\Html;
use app\assets\AppAsset;
AppAsset::register($this);
?>
Until now, we can test in the browser, we found no introduction css and js files to note here, we also need the final step:
In view of the file we'll add the code (Note: If we use public view file can be added to public view the file, if not used, can be placed in a separate page)
<?php
$this->beginPage()
?>
<?php $this->head() ?>
<?php $this->beginBody() ?>
<?php $this->endBody() ?>
<?php $this->endPage() ?>
4, the package does not need the resource manager in the methods defined, as long as the page in the view can be directly introduced
AppAsset::register($this);
//css定义一样
$this->registerCssFile('@web/css/font-awesome.min.css',['depends'=>['api\assets\AppAsset']]);
$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset']]);
//$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset'],'position'=>$this::POS_HEAD]);
Reprinted link: HTTPS: //www.jianshu.com/p/45426d8b2c0e
yii2 load css and js at the bottom of the page: http: //www.manks.top/article/yii2_load_js_css_in_end