DAY20 | 跟 Vue.js 認識的30天 - Vue 插件(Plugin)
這一篇說實在的,現在的我還不能寫出有用的插件,所以這一篇筆記主要也是學習使用別人寫好的插件。
使用插件
1 | Vue.use('PluginName', { |
使用別人寫好的插件
插件 : VueAxios
1 | import Vue from 'vue' |
其實在使用別人寫好的插件時,都會附使用說明,就照著說明使用即可。
就目前而言,比較常用的 Vue 插件就是 Vue-router 跟 Vuex ,但因為這2個插件只要檢測到存在 Vue 這個變量後,就會自動調用 Vue.use()
,所以不需要額外寫 Vue.use()
,但必須注意如果是使用 CommonJS 模塊化,仍得使用 Vue.use()
。
開發插件
可以將比較常用或可複用的方法寫在 Plugin 裡做整合,之後再透過 Vue.use()
一次性載入到專案中使用。
1 | const MyPlugin = { |
Demo
See the Pen DAY20 | 跟 Vue.js 認識的30天 - Vue 插件(Plugin) by Celeste6666 (@celeste6666) on CodePen.
參考資料