2

我创建了一个 testasset.php 如下:

<?php

namespace app\assets;
use yii\web\AssetBundle;

class TestAsset extends AssetBundle {
    /*public $basePath = '@webroot';
      public $baseUrl = '@web'; */
      public $sourcePath = '@app/assets';
      public $css = [
        '/css/test.css',
      ];
      public $js = [];
      public $depends = [
      /*  'yii\web\YiiAsset',
          'yii\bootstrap\BootstrapAsset', */
      ];
}

在控制器中,我只是渲染在资产上方注册的视图文件。

看法 :

<?php 

use app\assets\TestAsset;
TestAsset::register($this);
$this->beginPage(); ?>

<html>
  <head>
    <title>Register JS and CSS</title> <?php 
     //Yii::$app->view->registerCssFile('/css/style.css');?>
    <!-- <link rel="stylesheet" href="/css/test.css" type="text/css" /> -->
  </head>
  <body>
    <div id="page">
      <div id="logo">
        <h1><a href="/" id="logoLink">Register JS and CSS Test</a></h1>
      </div>
      <div id="nav">
        <ul>
          <li><a href="#">!</a></li>
          <li><a href="#">@</a></li>
          <li><a href="#">#</a></li>
        </ul>
      </div>
      <div id="content">
        <h2>Test</h2>
        <p> XYZ...!!! </p>
        <p> ABC...!!! </p>
      </div>
      <div id="footer"> 
        <p> Webpage made by <a href="/" target="_blank">XYZ</a> </p>
      </div>
    </div>
  </body>
</html>

但是我的 test.css 文件没有被注册。我也尝试过使用 basepath 和 baseurl 以及 registerCss Yii2 函数,但没有成功。

任何人请提出我的代码中缺少的地方。

4

3 回答 3

2

You need to add

<?php $this->head() ?>

preferably somewhere between <head> and </head> for the header asset files to be rendered.

于 2016-12-28T11:15:51.227 回答
1

只需将其添加到视图文件中

<?php 
   $this->registerCssFile('PATH_TO_FILE');
?>
于 2016-12-28T12:52:13.323 回答
0

我在我的一个高级应用程序模板项目中遇到了同样的问题。我已经将我的 layout/main.php 文件与Yii2 高级应用程序模板$this->endPage()上的相同文件进行了比较,发现文件末尾丢失了。它被错误地删除了,没有它,没有 js,ccs 资产工作。

于 2018-02-13T18:51:57.100 回答