php - 我如何在 Yii 中缩小除 jQuery 之外的所有 Assets ?

标签 php jquery yii yii2

Yii 可以缩小和压缩 JS。我想压缩所有应用程序的 JS 并使用 Google 托管的 jQuery。我该怎么做?

Yii 允许您指定 jQuery 的来源 http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles

但我已经在为 compressed assets 使用 bundles 键:

'bundles' => require(__DIR__ . '/' . (YII_ENV_PROD ? 'assets-prod.php' : 'assets-dev.php')),

assets-prod.php 是自动生成的。我尝试在压缩过程中用这个自定义 Assets 包

Assets .php
// Asset manager configuration:
'assetManager' => [
    'basePath' => '@webroot/assets',
    'baseUrl' => '@web/assets',
    'bundles' => [
      'yii\web\JqueryAsset' => [
        'sourcePath' => null,   // do not publish the bundle
        'js' => [
          '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js',
        ],
      ],
    ],
],

但是当我运行 yii asset assets.php config/assets-prod.php 时,它根本没有生成任何 jQuery 文件。这几乎就是我想要的,但是当我加载页面时,jQuery 完全消失了。没有提到 jQuery。它在 assets-prod.php 中创建了这个,这似乎是错误的

'yii\\web\\JqueryAsset' => [
    'sourcePath' => null,
    'js' => [],
    'css' => [],
    'depends' => [],
],

好的,然后我尝试了 Assets 映射 http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#asset-mapping .我将其放入 web.php

      'assetMap' => [
        'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js',
      ],  

但是,它仍然不会加载 jQuery。我还原了 assets.php 并再次运行 yii assets assets.php config/assets-prod.php ,但随后它又回到将 jQuery 放入一个大的缩小 JS 文件中.

最佳答案

你必须使用“baseUrl”属性,像这样:

class GoogleAsset extends AssetBundle{

    public $baseUrl = 'http://maps.googleapis.com/maps/api';

    public $js = [
        'js?sensor=false&language=ru-ru&region=ru-ru'
    ];

} 

关于php - 我如何在 Yii 中缩小除 jQuery 之外的所有 Assets ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431079/

相关文章:

javascript - 在 Flask 与 JS 中设置 cookie

javascript - 如何执行从ajax响应返回的javascript函数

ubuntu - 在 xampp for ubuntu 中安装 YII 框架

php - 创建一个以 API 为中心的应用程序,我自己的疑惑

php - Joomla 中的 MYSQL 搜索查询

php - 定期付款 Paypal Express Checkout .....

javascript - jquery 保持值(value)被点击的项目

jQuery(...).activeform 不是 Yii 中的函数

php - xamps 上的 MySQL 服务运行,然后在几秒钟后停止

php - codeigniter 中的 Like-Unlike 函数