在用vue構(gòu)建項(xiàng)目的過(guò)程中晚胡,我們有時(shí)會(huì)用到別人開(kāi)發(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是你組件的名稱(chēng)<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日")將會(huì)展示出來(lái)捌显。
注:date文件指的是自定義組件文件夾扶歪;index.js指的是組件的入口加載文件善镰;Date.vue指的是組件模板