Vue頁面加載完成后如何自動加載自定義函數(shù)
頁面加載完成后自動加載自定義函數(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(){ ? ? ? ? ? ? //自動加載indexs方法 ? ? ? ? ? ? this.indexs(); ? ? ? ? }
mounted
在模板渲染成html后調(diào)用,通常是初始化頁面完成后,再對html的dom節(jié)點進(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(){ ? ? ? ? ? ? //自動加載indexs方法 ? ? ? ? ? ? this.indexs(); ? ? ? ? }
vue之自執(zhí)行函數(shù)
總是糾結(jié)在寫不寫隨筆之間,自我感覺很菜,但是對源碼愛得深沉,就寫給自己看吧。
我在網(wǎng)上看了很多人寫的源碼,按照依賴的方式一個一個找包,再找函數(shù),我覺得太麻煩,復(fù)雜。所以直接看vue.js。
打開vue.js,是個自執(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,就會發(fā)現(xiàn)很多插件會判斷下
typeof window !== undefined ? window : this;
這種寫法更偏向于在js的window全局環(huán)境中使用。
接著看對外輸出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ù)掛載再全局對象上(global.Vue = factory());
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用element-resize-detector監(jiān)聽元素寬度變化方式
這篇文章主要介紹了vue使用element-resize-detector監(jiān)聽元素寬度變化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12前端實現(xiàn)Vue組件頁面跳轉(zhuǎn)的多種方式小結(jié)
這篇文章主要為大家詳細(xì)介紹了前端實現(xiàn)Vue組件頁面跳轉(zhuǎn)的多種方式,文中的示例代碼講解詳細(xì),具有一定的參考價值,有需要的小伙伴可以了解下2024-02-02SpringBoot結(jié)合Vue3實現(xiàn)簡單的前后端交互
本文主要介紹了SpringBoot結(jié)合Vue3實現(xiàn)簡單的前后端交互,結(jié)合實際案例,說明了如何實現(xiàn)前后端數(shù)據(jù)的交互,具有一定的?參考價值,感興趣的可以了解一下2023-08-08