Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

javascript - Yii2: Registering Asset Bundle vs registering external Js file

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;

and then adding the use statement like

use appassetsAppAsset;
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.

  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.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  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

All the features that makes bundles shine are found in docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...