Yii2如何向脚本标记添加参数

时间:2015-05-14 13:06:13

标签: php jquery yii2 yii2-advanced-app

JS认为获取渲染的内容如下:

<script src="/assets/a00ccd3f/jquery.min.js"></script>

我需要为其添加async="async"标记。

所以看起来应该是

<script src="/assets/a00ccd3f/jquery.min.js" async="async" ></script>

我该怎么做?

2 个答案:

答案 0 :(得分:15)

在您自己的AssetBundle中,您可以添加

public $jsOptions = [
    'async' => 'async',
];

如果要将其添加到Yii2 JqueryAsset包(或其他一些包),可以将其添加到配置的组件部分:

'assetManager' => [
    'bundles' => [
        'yii\web\JqueryAsset' => [
            'jsOptions' => [
                'async' => 'async'
            ],
        ],
    ],
],

答案 1 :(得分:0)

像这样

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace frontend\assets;
use yii\web\AssetBundle;
class ThemeCustomAsset extends AssetBundle
{
public $basePath    = '@webroot';
public $baseUrl     = '@web';
public $css = [];
public $js = [
    "js/lazysizes.min.js"
];
public $depends = [
    #'yii\web\YiiAsset',
    #'yii\bootstrap\BootstrapAsset',
];
public $jsOptions = ['async' => 'async'];
}