<i id='qazED'><tr id='qazED'><dt id='qazED'><q id='qazED'><span id='qazED'><b id='qazED'><form id='qazED'><ins id='qazED'></ins><ul id='qazED'></ul><sub id='qazED'></sub></form><legend id='qazED'></legend><bdo id='qazED'><pre id='qazED'><center id='qazED'></center></pre></bdo></b><th id='qazED'></th></span></q></dt></tr></i><div id='qazED'><tfoot id='qazED'></tfoot><dl id='qazED'><fieldset id='qazED'></fieldset></dl></div>
    1. <tfoot id='qazED'></tfoot>

      <small id='qazED'></small><noframes id='qazED'>

        <legend id='qazED'><style id='qazED'><dir id='qazED'><q id='qazED'></q></dir></style></legend>
          <bdo id='qazED'></bdo><ul id='qazED'></ul>
      1. Yii2:注册资产包与注册外部 Js 文件

        时间:2023-10-16
        • <small id='NdvPl'></small><noframes id='NdvPl'>

            <tbody id='NdvPl'></tbody>
          <i id='NdvPl'><tr id='NdvPl'><dt id='NdvPl'><q id='NdvPl'><span id='NdvPl'><b id='NdvPl'><form id='NdvPl'><ins id='NdvPl'></ins><ul id='NdvPl'></ul><sub id='NdvPl'></sub></form><legend id='NdvPl'></legend><bdo id='NdvPl'><pre id='NdvPl'><center id='NdvPl'></center></pre></bdo></b><th id='NdvPl'></th></span></q></dt></tr></i><div id='NdvPl'><tfoot id='NdvPl'></tfoot><dl id='NdvPl'><fieldset id='NdvPl'></fieldset></dl></div>
        • <tfoot id='NdvPl'></tfoot>

                <bdo id='NdvPl'></bdo><ul id='NdvPl'></ul>

                <legend id='NdvPl'><style id='NdvPl'><dir id='NdvPl'><q id='NdvPl'></q></dir></style></legend>

                1. 本文介绍了Yii2:注册资产包与注册外部 Js 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道按照文档中描述的过程注册 Asset Bundle 的好处,例如流程一在 AppAsset.php 中

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

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

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

                  then in the view file adding Namespace like

                  namespace appassets;
                  

                  然后添加像

                  use appassetsAppAsset;
                  AppAsset::register($this);
                  

                  如果我使用流程二

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

                  它工作正常.那么我为什么要使用流程一.

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

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

                  谢谢.

                  推荐答案

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

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

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

                  会产生类似的东西:

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

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

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

                  但是如果你启用了 urlManager,你的 url 将看起来像 http://localhost/yiiproject/user/update/8,这意味着你的浏览器会在以下位置寻找你的 js 文件:/yiiproject/user/update/8/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.

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

                  You could overcome this problem by using:

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

                  但是 Asset Bundle 基本上可以为您做到这一点.

                  But the Asset Bundle basicly does that for you.

                  这篇关于Yii2:注册资产包与注册外部 Js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                2. <legend id='Tjg3F'><style id='Tjg3F'><dir id='Tjg3F'><q id='Tjg3F'></q></dir></style></legend>

                    <i id='Tjg3F'><tr id='Tjg3F'><dt id='Tjg3F'><q id='Tjg3F'><span id='Tjg3F'><b id='Tjg3F'><form id='Tjg3F'><ins id='Tjg3F'></ins><ul id='Tjg3F'></ul><sub id='Tjg3F'></sub></form><legend id='Tjg3F'></legend><bdo id='Tjg3F'><pre id='Tjg3F'><center id='Tjg3F'></center></pre></bdo></b><th id='Tjg3F'></th></span></q></dt></tr></i><div id='Tjg3F'><tfoot id='Tjg3F'></tfoot><dl id='Tjg3F'><fieldset id='Tjg3F'></fieldset></dl></div>

                    <small id='Tjg3F'></small><noframes id='Tjg3F'>

                    <tfoot id='Tjg3F'></tfoot>
                      <bdo id='Tjg3F'></bdo><ul id='Tjg3F'></ul>
                            <tbody id='Tjg3F'></tbody>