yii2 中的资源包生成空文件

2022-08-30 17:18:16

资源包生成空的 JavaScript 和 CSS 文件

namespace frontend\assets;

return [

    'bundles' => [
        'frontend\assets\AppAsset',

    ],

    'targets' => [
        'frontend\assets\AppAsset' => [
            'basePath' => 'e:/path/yii2.loc/www',
            'baseUrl' => '',
            'js' => 'js/{ts}.js',
            'css' => 'css/{ts}.css',
        ],
    ],

    'assetManager' => [
        'basePath' => 'e:/path/yii2.loc/www/assets',
        'baseUrl' => '',
    ],
];

配置.php

return [

    'bundles' => [
        'frontend\assets\AppAsset',
    ],

    'targets' => [
        'frontend\assets\AppAsset' => [
            'basePath' => 'e:/path/yii2.loc/www',
            'baseUrl' => '',
            'js' => 'cache/{ts}.js',
            'css' => 'cache/{ts}.css',
        ],
    ],

    'assetManager' => [
        'basePath' => 'e:/path/yii2.loc/www/assets',
        'baseUrl' => '',
    ],
];

然后在控制台中

yii asset e:\path\config.php e:\path\compressed.php
//compresed.php it's result file with name of compressed files

在配置中

'assetManager' => [
    'bundles' => require dirname(__DIR__) . '/assets/compressed.php',
],

CSS 和 JavaScript 文件位于一个目录中:

e:/path/yii2.loc/www/css

e:/path/yii2.loc/www/js

捆绑包生成空值以:

e:/path/yii2.loc/www/cache/css and e:/path/yii2.loc/www/cache/js

我做错了什么?


答案 1

在配置.php,尝试配置组件“assetManager”,如以下 LOC 所示:

'components' => [
    'assetManager' => [
        'class' => 'yii\web\AssetManager', 
        'basePath' => 'YOUR_BASE_PATH' 
    ],  
],

答案 2

您应该将别名设置为@web和@webroot,因为此文件将在控制台脚本中使用。然后使用别名设置 basePath 和 baseUrl 参数

查看 https://www.yiiframework.com/doc/guide/2.0/en/structure-assets 的更多信息


推荐