类别:YII / 日期:2019-12-18 / 浏览:227 / 评论:0

引入体式格局有多种:

1、能够直接在视图页面上引入

<?php use yii\helpers\Html;?><?=Html::cssFile('@web/css/index.css')?><?=Html::jsFile('@web/js/jquery.min.js')?>

2、能够直接写原生代码引入,途径是项目目次/web/css 或许/js

<script src="js/nav.js"></script>

3、能够运用assetBundle治理css款式及js剧本

资本包定义: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 <qiang.xue@gmail.com>
 * @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']);  
    }  
}

在视图文件开头写入:

<?php
use yii\helpers\Html;use app\assets\AppAsset;
AppAsset::register($this); 
?>

到现在为止,我们能够在浏览器上测试,发明并没有引入css和js文件,这里要注重了,我们还需要末了一步:

在视图文件中我们要到场一下代码(注:假如我们运用大众视图文件,能够到场到大众视图文件,假如没有运用,能够放到零丁页面中)

<?php$this->beginPage() ?> 
<?php $this->head() ?>
<?php $this->beginBody() ?> 
<?php $this->endBody() ?>
<?php $this->endPage() ?>

4、不需要在资本包治理器中定义要领,只要在视图页面中直接引入即可

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]);

相干文章教程引荐:yii教程

以上就是yii2怎样引入css和js文件的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章