javascript Yii2:注册资产包与注册外部 Js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28140166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:29:02  来源:igfitidea点击:

Yii2: Registering Asset Bundle vs registering external Js file

javascriptphpjqueryyii2

提问by Pawan

Hi I wanted to know the advantage of registering Asset Bundle following the process described in the docs like Process onein AppAsset.php

嗨,我想知道按照 AppAsset.php 中的Process one等文档中描述的流程注册 Asset Bundle 的 好处

public $js = [
        'js/myjsfile.js'
    ];

then in the view file adding Namespace like

然后在视图文件中添加命名空间,如

namespace app\assets;

and then adding the use statement like

然后添加 use 语句,如

use app\assets\AppAsset;
AppAsset::register($this);

Instead of doing all this if I use Process Two

如果我使用流程二,而不是做所有这些

$this->registerJs('js/myjsfile.js', $this::POS_READY);

it works fine. So why should I use Process One.

它工作正常。那么我为什么要使用Process One

  1. Any advantage and reason for this will be greatly appreciated.
  2. If I follow the process oneDo I need to add all the js files in AppAsset.php individually.
  1. 任何优势和原因将不胜感激。
  2. 如果我按照流程一,我是否需要单独添加 AppAsset.php 中的所有 js 文件。

Thanks.

谢谢。

采纳答案by deacs

One of the main reasons for using an Asset Bundle is that your assets' paths will always be correct. Consider:

使用 Asset Bundle 的主要原因之一是您的资产路径始终是正确的。考虑:

$this->registerJsFile('js/myjsfile.js', ['position'=>$this::POS_READY]);

will generate something like:

会产生类似的东西:

<script src="js/myjsfile.js"></script>

Which works great for non urlManager enabled urls, e.g. http://localhost/yiiproject/index.php?r=user/update&id=8because your browser looks for the js file at: /yiiproject/js/myjsfile.js

这对于未启用 urlManager 的 url 非常有用,例如http://localhost/yiiproject/index.php?r=user/update&id=8因为您的浏览器在以下位置查找 js 文件:/yiiproject/js/myjsfile.js

But if you enable urlManager, your url will look like http://localhost/yiiproject/user/update/8, which means your browser will look for your js file at: /yiiproject/user/update/8/js/myjsfile.js.

但是如果您启用 urlManager,您的 url 将看起来像http://localhost/yiiproject/user/update/8,这意味着您的浏览器将在以下位置查找您的 js 文件:/yiiproject/user/update/8/js/myjsfile.js

You could overcome this problem by using:

您可以使用以下方法解决此问题:

$this->registerJsFile(Yii::$app->request->baseUrl.'/js/myjsfile.js', ['position'=>$this::POS_READY]);

But the Asset Bundle basicly does that for you.

但是 Asset Bundle 基本上是为你做的。

回答by Stefano Mtangoo

Asset Bundles have some advantages over normal registering. Apart from what @deacs said in his/her answer here are others:

资产包比普通注册有一些优势。除了@deacs 在他/她的回答中所说的,还有其他的:

  1. Assets Bundles can publish the file to assets if its not in web accessible directory
  2. Assets Bundle can deal with less files (in case of CSS) as well as compressing the assets.
  3. Makes Code Elegant especially in solving dependencies and hence reusability
  1. 如果文件不在 Web 可访问目录中,资产包可以将文件发布到资产
  2. Assets Bundle 可以处理更少的文件(在 CSS 的情况下)以及压缩资产。
  3. 使代码优雅,尤其是在解决依赖关系和可重用性方面

All the features that makes bundles shine are found in docs

使 bundle 大放异彩的所有功能都可以在文档中找到

回答by Bruno de Oliveira

Using Asset Bundles, you can also get the latest version from 'vendor' folder, so if you need to update some lib you don't need to manually do this since composer already do this.

使用 Asset Bundles,您还可以从“vendor”文件夹中获取最新版本,因此如果您需要更新某些库,则无需手动执行此操作,因为 composer 已经执行了此操作。