1:  关于vue.use 用法详解:

Vue使用别人的组件时,会用到 Vue.use () 用法: 例如:Vue.use(VueRouter)Vue.use(MintUI); 但是用 axios时,就不需要用 Vue.use(axios)。 

因为 axios 没有 install方法。 接下来我们自定义一个需要 Vue.use() 的组件,也就是有 install 的组件,看完之后就明白了。

2: 定义组件:

生成模版 vue init webpack-simple custom-global-component
custom-global-component 为新建的文件夹名称
然后一路回车
cd custom-global-component 进入该文件夹
npm install 安装本次需要的模块
npm run dev 运行项目
如果能正常打开,进行下一步

作者:刘员外__
链接:https://www.jianshu.com/p/89a05706917a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3: 在 Loading.vue 中定义一个组件: 定义 Loading.vue 中定义一个组件:

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

4:  在 index.js 中 引入 Loading.vue ,并导出

1: 首先引入组件:
 import LoadingComponent from './loading.vue'
2: const loading = {
    // install 是一个方法,当外界use 这个组件的时候:就会调用本身的install 方法; 同时传vue 这个类方法; 

  install: function(Vue) {
    Vue.component('loading', LoadingComponent)   // 组件

  }
 }
3: 导出:
  export default Loading;

5:  在 main.js 中引入 loading 文件下的 index;

// 其中'./components/loading/index' 的 /index 可以不写,webpack会自动找到并加载 index 。如果是其他的名字就需要写上。
import Loading from './components/loading/index'
// 这时需要 use(Loading),如果不写 Vue.use()的话,浏览器会报错,大家可以试一下
Vue.use(Loading)

6: 在App.vue里面写入定义好的组件标签 <Loading></Loading>

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

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐