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

接著,我們在 src/components 目錄下創(chuàng)建一個自定義的組件 splash.vue ,示例代碼如下所示:
<template>
<div>
<p>{{ title }}</p>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
name: "splash",
props:['title'],
data: function () {
return {
message: "組件啟動中...",
};
},
};
</script>
注冊成局部組件
我們知道,通過 Vue-CLI 創(chuàng)建的項目,vue 會自動為我們創(chuàng)建一個 App.vue 的根組件,用于承載整個應(yīng)用程序,如果我們在 App 的 components 注冊我們的自定義組件,那么它也算一個局部組件,只能應(yīng)用在 App 這樣的一個組件內(nèi)。注冊方式如下所示:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
<!-- 3.應(yīng)用自定義組件 -->
<splash title="hello world"></splash>
</div>
</template>
<script>
// 1.導入組件
import Splash from "./components/Splash.vue";
export default {
name: "App",
components: {
// 2.注冊局部組件
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 ,會出現(xiàn)如下所示的效果:

注冊成全局組件
如果想將我們的組件注冊成全局組件,共其它所有組件都能使用的話,我們就需要將我們的組件注冊成全局組件。同樣的,我們需要在 'main.js' 中注冊我們的全局組件,示例代碼如下所示:
import Vue from 'vue'
import App from './App.vue'
// 1. 導入自定義組件
import Splash from './components/Splash.vue'
Vue.config.productionTip = false
// 2. 將自定義組件注冊成全局組件
Vue.component(Splash.name, Splash)
new Vue({
render: h => h(App),
}).$mount('#app')
修改 HelloWorld.vue ,使用上面已經(jīng)注冊的全局組件,示例代碼如下所示:
<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)注冊的全局組件,示例代碼如下所示:
<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 ,會出現(xiàn)如下所示的效果:

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

參考鏈接
|