欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue.use自定義自己的全局組件

 更新時間:2017年06月14日 14:56:17   作者:22337383  
本篇文章主要介紹了Vue.use自定義自己的全局組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

通常我們在vue里面使用別人開發(fā)的組件,第一步就是install,第二步在main.js里面引入,第三步Vue.use這個組件。今天我簡單的也來use一個自己的組件。

這里我用的webpack-simple這個簡單版本的腳手架為例,安裝就不啰嗦了,直接進入正題

首先看下目前的項目結(jié)構(gòu):

webpack首先會加載main.js,所以我們在main的js里面引入。我以element ui來做對比說明

import Vue from 'vue'
import App from './App.vue'

// 引入element-ui組件
import ElementUi from 'element-ui'
import 'element-ui/lib/theme-default/index.css'

// 引入自定義組件。index.js是組件的默認入口
import Loading from '../components/loading'
Vue.use(Loading);

Vue.use(ElementUi);
new Vue({
 el: '#app',
 render: h => h(App)
})

然后在Loading.vue里面定義自己的組件模板

<!-- 這里和普通組件的書寫一樣 -->
<template>
  <div class="loading">
    loading...
  </div>
</template>

在index.js文件里面添加install方法

import MyLoading from './Loading.vue'
// 這里是重點
const Loading = {
  install: function(Vue){
    Vue.component('Loading',MyLoading)
  }
}

// 導(dǎo)出組件
export default Loading

接下來就是在App.Vue里面使用組件了,這個組件已經(jīng)在main.js定義加載了

<template>
 <div id="app">
 <!-- 使用element ui的組件 -->
 <el-button>默認按鈕</el-button>

 <!-- 使用自定義組件 -->
 <Loading></Loading>
 </div>
</template>

下面是效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論