
創(chuàng)建項(xiàng)目
通過(guò) cmd 執(zhí)行如下示例命令來(lái)初始化我們的 Vue 項(xiàng)目
vue create helloworld
cd helloworld
code .
npm run serve
命令執(zhí)行完成后,項(xiàng)目結(jié)構(gòu)如下圖所示:

接著,我們?cè)?src/components 目錄下創(chuàng)建一個(gè)自定義的組件 splash.vue ,示例代碼如下所示:
<template>
<div>
<p>{{ title }}</p>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
name: "splash",
props:['title'],
data: function () {
return {
message: "組件啟動(dòng)中...",
};
},
};
</script>
注冊(cè)成局部組件
我們知道,通過(guò) Vue-CLI 創(chuàng)建的項(xiàng)目,vue 會(huì)自動(dòng)為我們創(chuàng)建一個(gè) App.vue 的根組件,用于承載整個(gè)應(yīng)用程序,如果我們?cè)?App 的 components 注冊(cè)我們的自定義組件,那么它也算一個(gè)局部組件,只能應(yīng)用在 App 這樣的一個(gè)組件內(nèi)。注冊(cè)方式如下所示:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
<!-- 3.應(yīng)用自定義組件 -->
<splash title="hello world"></splash>
</div>
</template>
<script>
// 1.導(dǎo)入組件
import Splash from "./components/Splash.vue";
export default {
name: "App",
components: {
// 2.注冊(cè)局部組件
Splash,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
執(zhí)行 npm run serve ,會(huì)出現(xiàn)如下所示的效果:

注冊(cè)成全局組件
如果想將我們的組件注冊(cè)成全局組件,共其它所有組件都能使用的話,我們就需要將我們的組件注冊(cè)成全局組件。同樣的,我們需要在 'main.js' 中注冊(cè)我們的全局組件,示例代碼如下所示:
import Vue from 'vue'
import App from './App.vue'
// 1. 導(dǎo)入自定義組件
import Splash from './components/Splash.vue'
Vue.config.productionTip = false
// 2. 將自定義組件注冊(cè)成全局組件
Vue.component(Splash.name, Splash)
new Vue({
render: h => h(App),
}).$mount('#app')
修改 HelloWorld.vue ,使用上面已經(jīng)注冊(cè)的全局組件,示例代碼如下所示:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<!-- 應(yīng)用自定義組件 -->
<splash></splash>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String,
},
};
</script>
<style scoped>
.hello {
border: 1px dashed #00f;
}
</style>
修改 App.vue ,使用上面已經(jīng)注冊(cè)的全局組件,示例代碼如下所示:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
<splash></splash>
<hello-world msg="我是子組件"></hello-world>
</div>
</template>
<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
name: "App",
components: {
HelloWorld,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
執(zhí)行 npm run serve ,會(huì)出現(xiàn)如下所示的效果:

注冊(cè)成插件
由于插件更具靈活性,所以我們可以自定義組件注冊(cè)成全局組件。按照 Vue 的約定,我們需要將我們的項(xiàng)目結(jié)構(gòu)做一下調(diào)整。
在 src/plugin 目錄下建一個(gè)和組件名稱(chēng)一致的文件夾,然后將我們上面定義的 splash.vue 文件放到這個(gè)目錄下面,然后在這個(gè)目錄下面再建一個(gè) index.js 的文件,通過(guò)在這個(gè)文件里面寫(xiě)注冊(cè)代碼,將我們的自定義組件注冊(cè)成插件。示例代碼如下所示:
import Splash from './Splash'
export default {
install: function (Vue, options) {
// 1.獲取構(gòu)造函數(shù)
const contructor = Vue.extend(Splash)
// 2. 實(shí)例化組件對(duì)象
const instance = new contructor()
// 3. 創(chuàng)建頁(yè)面元素
const oDiv = document.createElement('div')
document.body.appendChild(oDiv)
// 4. 將組件掛載到頁(yè)面元素上
instance.$mount(oDiv)
if (options !== null && options.title !== undefined) {
instance.title = options.title
}
// 添加全局方法
Vue.ToggleSplash = function () {
instance.isShow = !instance.isShow;
}
// 添加實(shí)例方法
Vue.prototype.$ToggleSplash = function () {
instance.isShow = !instance.isShow;
}
}
}
修改 main.js ,示例代碼如下所示:
import Vue from 'vue'
import App from './App.vue'
// 1. 導(dǎo)入自定義組件
import Splash from './plugin/splash/index'
Vue.config.productionTip = false
// 2. 將自定義組件注冊(cè)成組件
Vue.use(Splash, { title: 'hello world' })
new Vue({
render: h => h(App),
}).$mount('#app')
接下來(lái),我們就可以在 任何組件中通過(guò)調(diào)用 Vue 對(duì)象的全局方法或?qū)嵗椒▉?lái)控制我們的自定義組件,比如,我們可以在 hello-world 這樣使用:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<button @click="toggle1">使用全局方法控制顯示或隱藏插件</button>
<button @click="toggle2">使用實(shí)例方法控制顯示或隱藏插件</button>
</div>
</template>
<script>
import Vue from "vue";
export default {
name: "HelloWorld",
props: {
msg: String,
},
methods: {
toggle1: function () {
// 通過(guò)全局方法來(lái)訪問(wèn)自定義組件
Vue.ToggleSplash();
},
toggle2: function () {
// 通過(guò)實(shí)例方法來(lái)訪問(wèn)自定義組件
this.$ToggleSplash();
},
},
};
</script>
<style scoped>
.hello {
border: 1px dashed #00f;
}
</style>
執(zhí)行 npm run serve ,會(huì)出現(xiàn)如下所示的效果:

參考鏈接
|