在用vue構(gòu)建項(xiàng)目的過程中,我們有時(shí)會用到別人開發(fā)的組件如vue-router;使用他人組件的正常步驟如下:
1、命令行進(jìn)行安裝,執(zhí)行install;
2、在vue項(xiàng)目中的入口文件main.js中,進(jìn)行導(dǎo)入;
3、然后用Vue.use(plugin)引入該組件。
自定義組件具體步驟如下:
1、在components文件中創(chuàng)建一個(gè)文件,如:date;
2、在date文件中,創(chuàng)建index.js和Date.vue;
3、Date.vue中的代碼如下:
<template>
<div>
<div>2019年7月14日</div>
</div>
</template>
<script>
export default{
data () {
return {
msg: 'hello vue'
}
},
}
</script>
4、date文件夾下的index.js中的代碼如下:
import Date1 from "./Date.vue";
const Dates = {
install(Vue) {//必須有install,vue是new Vue構(gòu)造函數(shù)
Vue.component("Date", Date1)
//Date是你組件的名稱<Date></Date> Date1是你引入的時(shí)候命名的
}
}
export default Dates
5、入口文件main.js進(jìn)行相關(guān)的配置:
import Date from './components/date' //引入文件夾名字默認(rèn)是引入index.js
Vue.use(Date)
6、如此這般,就可以在其它組件中正常使用,如下:
<template>
<div class="hello">
<Date></Date>
</div>
</template>
自定義組件Date的內(nèi)容("2019年7月14日")將會展示出來。
注:date文件指的是自定義組件文件夾;index.js指的是組件的入口加載文件;Date.vue指的是組件模板