Vue.use() 方法


1.本人在學習Vue時,會用到 Vue.use() 。例如:Vue.use(VueRouter)Vue.use(MintUI)。但是用 axios時,就不需要用 Vue.use(axios),就能直接使用。那這是為什么吶?然后就自己找原因。

答案: 因為 axios 沒有 install。接下來我們自定義一個需要 Vue.use() 的組件,看完就明白了。

 

2.在 Loading.vue 中定義一個組件

<template> <div class="loading-box"> Loading... </div> </template>

3.在 index.js 中 引入 Loading.vue ,並導出

// 引入組件 import LoadingComponent from './loading.vue' // 定義 Loading 對象 const Loading={ // install 是默認的方法。當外界在 use 這個組件的時候,就會調用本身的 install 方法,同時傳一個 Vue 這個類的參數。 install:function(Vue){ Vue.component('Loading',LoadingComponent) } } // 導出 export default Loading 

4.在 main.js 中引入 loading 文件下的 index

// 其中'./components/loading/index' 的 /index 可以不寫,webpack會自動找到並加載 index 。如果是其他的名字就需要寫上。 import Loading from './components/loading/index' // 這時需要 use(Loading),如果不寫 Vue.use()的話,瀏覽器會報錯,大家可以試一下 Vue.use(Loading) 

5.在App.vue里面寫入定義好的組件標簽 <Loading></Loading>

<template> <div id="app"> <h1>vue-loading</h1> <Loading></Loading> </div> </template>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM