欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue頁面加載完成后如何自動(dòng)加載自定義函數(shù)

 更新時(shí)間:2022年07月02日 08:42:28   作者:光頭強(qiáng)兒  
這篇文章主要介紹了Vue頁面加載完成后如何自動(dòng)加載自定義函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

頁面加載完成后自動(dòng)加載自定義函數(shù)

created

在模板渲染成html前調(diào)用,即通常初始化某些屬性值,然后再渲染成視圖。

methods: {
? ? ? ? ? ? indexs:function(){
? ? ? ? ? ? ? ? this.$http.post('{:url("Index/fun")}')
? ? ? ? ? ? ? ? ? ? .then(function(res){
? ? ? ? ? ? ? ? ? ? ? ? this.items=res.data;
? ? ? ? ? ? ? ? ? ? ? ? console.log(res.data);
? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? .catch(function(error){
? ? ? ? ? ? ? ? ? ? ? ? console.log(error);
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? created(){
? ? ? ? ? ? //自動(dòng)加載indexs方法
? ? ? ? ? ? this.indexs();
? ? ? ? }

mounted

在模板渲染成html后調(diào)用,通常是初始化頁面完成后,再對(duì)html的dom節(jié)點(diǎn)進(jìn)行一些需要的操作。

methods: {
? ? ? ? ? ? indexs:function(){
? ? ? ? ? ? ? ? this.$http.post('{:url("Index/fun")}')
? ? ? ? ? ? ? ? ? ? .then(function(res){
? ? ? ? ? ? ? ? ? ? ? ? this.items=res.data;
? ? ? ? ? ? ? ? ? ? ? ? console.log(res.data);
? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? .catch(function(error){
? ? ? ? ? ? ? ? ? ? ? ? console.log(error);
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? mounted(){
? ? ? ? ? ? //自動(dòng)加載indexs方法
? ? ? ? ? ? this.indexs();
? ? ? ? }

vue之自執(zhí)行函數(shù)

總是糾結(jié)在寫不寫隨筆之間,自我感覺很菜,但是對(duì)源碼愛得深沉,就寫給自己看吧。

我在網(wǎng)上看了很多人寫的源碼,按照依賴的方式一個(gè)一個(gè)找包,再找函數(shù),我覺得太麻煩,復(fù)雜。所以直接看vue.js。

打開vue.js,是個(gè)自執(zhí)行函數(shù),也就是IIFE。

(function(global,factory){
? ? typeof exports === 'object' && typeof module !== 'undefined'?
? ? ? ? ? module.exports = factory
? ? ? ? : typeof define === 'function' && define.amd?
? ? ? ? ? ? ? define(factory)
? ? ? ? ? ? : (global.Vue = factory())
})(this,function(){
? ? 'use strict'
})

自執(zhí)行函數(shù)想必不用我多說了,讓我們來分析下這種插件與框架的寫法。

它的參數(shù)為global和factory,在js環(huán)境下也就是window和Vue的構(gòu)造函數(shù)。

this在這里值window,如果經(jīng)??丛创a,就會(huì)發(fā)現(xiàn)很多插件會(huì)判斷下

typeof window !== undefined ? window : this;

這種寫法更偏向于在js的window全局環(huán)境中使用。

接著看對(duì)外輸出factory,首先判斷 module和exports存在的情況

typeof exports === 'object' && typeof module !== 'undefined'

也就是優(yōu)先使用AMD(module.exports = factory),接著判斷CMD是否存在

typeof define === 'function' && define.amd?

若AMD不存在而CMD存在,則使用CMD(define(factory)),若AMD,CMD都不存在,就把Vue的構(gòu)造函數(shù)掛載再全局對(duì)象上(global.Vue = factory());

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論