1、在 assets/AppAsset 里定义方法

<?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 = [ ]; // 全局
public $js = [ ]; public $depends = [
// 'yii\web\YiiAsset',
// 'yii\bootstrap\BootstrapAsset', // 注释掉禁用bootstrap
]; // 这是设置所有js放置的位置
public $jsOptions = [
'position' => \yii\web\View::POS_HEAD,
]; //定义按需加载JS方法
public static function addJs($view, $jsfile) {
$view->registerJsFile(
$jsfile,
[
AppAsset::className(),
"depends" => "app\assets\AppAsset"
]
);
} //定义按需加载css方法
public static function addCss($view, $cssfile) {
$view->registerCssFile(
$cssfile,
[
AppAsset::className(),
"depends" => "app\assets\AppAsset"
]
);
} }

2、在view里调用

<?php

/* @var $this \yii\web\View */
/* @var $content string */ use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset; // 注册全局加载
AppAsset::register($this); // 按需加载css
AppAsset::addCss($this, Yii::$app->request->baseUrl."/css/site.css");
// 按需加载js
AppAsset::addJs($this, Yii::$app->request->baseUrl."/js/respond.min.js"); ?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?> <?= $content ?> <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

  

yii2 加载静态资源的更多相关文章

  1. Django的urls.py加载静态资源图片,TypeError: view must be a callable or a list/tuple in the case of include().

    Django的urls.py加载静态资源图片,TypeError: view must be a callable or a list/tuple in the case of include(). ...

  2. flask模板应用-加载静态文件:添加Favicon,使用CSS框架,使用宏加载静态资源

    加载静态文件 一个Web项目不仅需要HTML模板,还需要许多静态文件,比如CSS.JavaScript文件.图片和声音声.在flask程序中,默认需要将静态文件存储在与主脚本(包含程序实例的脚本)同级 ...

  3. Thymeleaf模板引擎绕过浏览器缓存加载静态资源js,css文件

    浏览器会缓存相同文件名的css样式表或者javascript文件.这给我们调试带来了障碍,好多时候修改的代码不能在浏览器正确显示. 静态常见的加载代码如下: <link rel="st ...

  4. Flask之加载静态资源

    Flask之加载静态资源 1.加载css样式 <link rel="stylesheet" href="{{ url_for('static',filename=' ...

  5. nginx反向代理部署springboot项目报404无法加载静态资源

    问题:nginx反向代理部署springboot项目报404无法加载静态资源(css,js,jpg,png...) springboot默认启动端口为8080,如果需要通过域名(不加端口号)直接访问s ...

  6. 关于springboot2.*版本无法加载静态资源

    前言 在学习springboot的过程中,发现无法引用静态资源.我使用的是springboot2.2.1版本. 追溯源码,终于解决.并记录下解决思路. 默认加载路径 首先得知道springboot默认 ...

  7. 页面性能优化:preload预加载静态资源

    本文主要介绍preload的使用,以及与prefetch的区别.然后会聊聊浏览器的加载优先级. preload 提供了一种声明式的命令,让浏览器提前加载指定资源(加载后并不执行),在需要执行的时候再执 ...

  8. Spring 加载静态资源

    <mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...

  9. Tomcat 部署项目无法加载静态资源

    首先,我的Eclipse是引用外部的Tomcat 引用外部Tomcat会在左侧生成一个Server文件夹,相当于复制了一份Tomcat到Eclipse的安装目录里 具体Tomcat所在目录可以在这进行 ...

随机推荐

  1. 从Inception v1,v2,v3,v4,RexNeXt到Xception再到MobileNets,ShuffleNet,MobileNetV2

    from:https://blog.csdn.net/qq_14845119/article/details/73648100 Inception v1的网络,主要提出了Inceptionmodule ...

  2. BigDecimal 实际测试结果

    package com.zzzy; import java.math.BigDecimal; public class Test { public static void main(String[] ...

  3. UVA-12293(组合游戏)

    题意: 有两个相同的盒子,一个盒子里面有n个球,另一个盒子里面有1个球,每次清空球较少的那个盒子,然后从另外的一个盒子里拿到空盒子里使得操作后两个盒子至少有一个球,判断是先手还是后者胜; 思路: 跟每 ...

  4. XAMPP的端口被占用

    打开xampp\apache\conf\httpd.conf文件把80端口修改为:8081;打开xampp\apache\conf\extre\httpd-ssl.conf文件把443修改为4433或 ...

  5. android 怎么实现跑马灯效果

    自定义控件 FocusedTextView, 使android系统误以为它拥有焦点 public class FocusedTextView extends TextView { public Foc ...

  6. 一、MyBatis简介

    1.发展历史 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...

  7. web面试常见问题

    1事件继承 function ClassA(sColor) {     this.color = sColor;     this.sayColor = function () {        al ...

  8. MFC绘制直角坐标系

    void CMyPicoTestDlg::DrawWave(CDC *pDC,CRect &rectPicture) { CPen newPen; //用于创建新画笔 CPen *pOldPe ...

  9. 华为codecraft2018总结

    华为codecraft2018总结 想来也是参加了第二次了,自己还是那么的菜.总结下今年的比赛,得奖是不存在的了,但是收获还是有的. 代码相关的都在这里了:https://github.com/hui ...

  10. C杂谈

    最近在做关于C的项目开发,记录一下有关C的操作,比较杂乱 1.利用System进行文件数量统计: 1) system("dir /b /s /ad d:\\mydir\\*.* | find ...