在线一区二区三区高清视频,国产精品妇女一二三区,美女被遭强高潮网站在线播放,实拍各种胸走光见奶头

關(guān)于Vue.use()詳解

時(shí)間:2021-08-09 11:21:41 類(lèi)型:vue
字號(hào):    

  問(wèn)題

  相信很多人在用Vue使用別人的組件時(shí),會(huì)用到 Vue.use() 。例如:Vue.use(VueRouter)、Vue.use(MintUI)。但是用 axios時(shí),就不需要用 Vue.use(axios),就能直接使用。那這是為什么吶?

  答案

  因?yàn)?axios 沒(méi)有 install。

  什么意思呢?接下來(lái)我們自定義一個(gè)需要 Vue.use() 的組件,也就是有 install 的組件,看完之后就明白了。

  定義組件

 生成模版 vue init webpack-simple custom-global-component

  custom-global-component 為新建的文件夾名稱(chēng)

  然后一路回車(chē)

  cd custom-global-component 進(jìn)入該文件夾

  npm install 安裝本次需要的模塊

  npm run dev 運(yùn)行項(xiàng)目

  如果能正常打開(kāi),進(jìn)行下一步

這是當(dāng)前項(xiàng)目目錄:

1.jpg

項(xiàng)目目錄-1.jpg

1.創(chuàng)建如下圖中的文件夾和文件

2.jpg

2.jpg

  3.在 index.js 中 引入 Loading.vue ,并導(dǎo)出

// 引入組件import LoadingComponent from './loading.vue'// 定義 Loading 對(duì)象const Loading={
    // install 是默認(rèn)的方法。當(dāng)外界在 use 這個(gè)組件的時(shí)候,就會(huì)調(diào)用本身的 install 方法,
    同時(shí)傳一個(gè) Vue 這個(gè)類(lèi)的參數(shù)。
    install:function(Vue){
        Vue.component('Loading',LoadingComponent)
    }}// 導(dǎo)出export default Loading

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

// 其中'./components/loading/index' 的 /index 可以不寫(xiě),webpack會(huì)自動(dòng)找到并加載 index 。
如果是其他的名字就需要寫(xiě)上。
import Loading from './components/loading/index'
// 這時(shí)需要 use(Loading),如果不寫(xiě) Vue.use()的話(huà),瀏覽器會(huì)報(bào)錯(cuò),大家可以試一下
Vue.use(Loading)

5.在App.vue里面寫(xiě)入定義好的組件標(biāo)簽 <Loading></Loading>

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

  6.看到這兒大家應(yīng)該就明白了吧,用 axios時(shí),之所以不需要用 Vue.use(axios),就能直接使用,是因?yàn)殚_(kāi)發(fā)者在封裝 axios 時(shí),沒(méi)有寫(xiě) install 這一步。至于為啥沒(méi)寫(xiě),那就不得而知了。

  作者:劉員外__

  鏈接:https://www.jianshu.com/p/89a05706917a

  


<