Fortune Telling Collection - Free divination - How to introduce JS package file into Yii2.0
How to introduce JS package file into Yii2.0
First, inline script registerJs ()
Use as follows:
//internally registered js code $ JSString = "$ (function () {alert (123); });" ; $ this-& gt; registerjs($jsString,View::POS _ END); Or $ this->; registerJs("var options = "。 json_encode($options)。 ";" , View::POS_END,' My-option');
Description:
The first element is the js code that we want to write in the view file.
The second element is where we decided to insert this code in the view file. The specific parameters are as follows:
View::POS_HEAD? Used of the head.
View::POS_BEGIN? Right after it was opened? & ltbody & gt。
View::POS_END? Just before closing time? & lt/body & gt; .
View::POS_READY? Used to execute code on a document? Are you ready? Events. Will this be registered? jQuery? Automatically.
View::POS_LOAD? Used to execute code on a document? Load? Events. Will this be registered? jQuery? Automatically.
The third element is the Id of this code, which is unique. If there is an ID, it will replace or replace the previous one with the same name. If the last element is not written, the js code itself is the ID, and the last element can be ignored.
This refers to the yii\web\View object, which is used to manage and present views.
There is such a way of writing.
& lt? PHP $ this-& gt; beginBlock("aaa ")? & gt
$(function () {? //Write your js code here}); & lt? PHP $ this-& gt; endBlock()? & gt
& lt? PHP $ this-& gt; register js($ this-& gt; blocks["aaa"],\ yii \ web \ View::POS _ END); ? & gt
A data block can specify that the view content is displayed in one place and in another place, and is usually used with layout.
For example, you can define a data block in the content view to display it in the layout.
Call yii\base\View::beginBlock () and yii\base\View::endBlock () to define the data block.
Use $ view-& gt;; Blocks[$blockID] accesses data blocks.
Where $blockID is the unique identification ID specified when defining the data block.
It can be seen that the internal writing is quite flexible ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Second, the external script registerJsFile ()
Use as follows:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
Use backend \ assets \ AppAsset.
Use yii \ web \ View
app asset::register($ this); //Introduce the js file $ this-& gt;; register jsfile(Yii::$ app-& gt; Request-> baseUrl。 js/mytest_js.js ',[' depends ' = & gt; back end \ assets \ app asset::class name()," position " = & gt$ this::POS _ END]);
Description: Import an external js file.
The first parameter is the absolute path of the file? yii::$ app-& gt; Request-> BaseUrl plus js file?
The second element is ribbon. It depends on who? Here, it is dependent. Back \ assets \ appasset:: classname () object.
The third element is the location of the import file, POS_END? Describe in
But it is usually not used to import external files, because it is relatively complicated to specify who to rely on every time you import a file. Usually, we use the package management asset bundle to register.
First of all: in this document? Back \ Assets \ AppAsset.php shows that we have introduced two static methods. The complete version of the appasset class is as follows:
Namespace Backend \ Assets; ?
Use yii \ web \ AssetBundle?
Class AppAsset extends AssetBundle? { ?
public $ basePath = ' @ webroot?
public $ baseUrl = ' @ web?
//Global CSS?
public $css = [?
css/animate.css ',?
' css/style.min.css ',?
];
//global JS?
public $js = [?
js/jquery-2. 1. 1.js '?
];
//Dependence?
public $depends = [?
' yii\web\YiiAsset ',?
yii\bootstrap\BootstrapAsset ',?
]; ?
//Define the method of loading JS on demand, and pay attention to the final loading order?
Public static function addScript($view, $jsfile) {?
$ view-& gt; registerJsFile($jsfile,[AppAsset::className(),' depends ' = & gtback end \ assets \ app asset ']); ?
} ?
?
//Define the method of loading css on demand, and pay attention to the final loading order?
Public static function addCss($view, $cssfile) {?
$ view-& gt; registerCssFile($cssfile,[AppAsset::className(),' depends ' = & gtback end \ assets \ app asset ']); ?
} ?
}
addScript()? And addCss () methods are the keys to our registration in the view file. We don't need to use them by using the following methods? $ this-& gt; registerJsFile? Registered.
& lt? Phpuse backend \ assets \ AppAsset uses yii \ web \ View.
app asset::register($ this);
//css file appasset:: addcss ($ this, yii:: $ app->; Request-> baseUrl。 "/CSS/blog . CSS ");
AppAsset::addScript($this,Yii::$ app-& gt; Request-> baseUrl。 /js/waibu _ js . js’);
- Previous article:The Growth Story of China Celebrities
- Next article:A Brief Introduction to The Princess of Blood
- Related articles
- Wang Yong supermarket is delicious.
- Liverpool/kloc beat wolves 0/-0. Who do you think performed well?
- The last words of the second wife's life and death
- The history of Chinese characters
- What is "three media, six certificates, three aunts and six grandmothers"?
- Capricorn 2023 Spring Festival XiShen bearing XiShen bearing query?
- Psychic warfare Lao Li candles cost hundreds of dollars.
- Lunar calendar 65438+ solar calendar February 26th 65438+1October 22nd at 20 o'clock 15 What is the rising constellation of Aquarius?
- How to make a quiet book by yourself
- How to get started with metaphysics and geomantic omen?