組件是Vue.js中一個(gè)非常強(qiáng)大的功能勿侯,可以拓展HTML元素拓瞪,封裝可重用的代碼。
1助琐、將組件內(nèi)容定義到template模板中
2祭埂、組件中實(shí)現(xiàn)指令及事件綁定
第一步:創(chuàng)建template
<template id="account">
<div>
<h3>組件內(nèi)容:{{msg}}(使用指令)</h3>
<a href="" @click="login">賬號(hào)(事件綁定)</a>
<a href="">密碼</a>
</div>
</template>
第二步:定義和注冊(cè)一個(gè)組件,指令,監(jiān)聽(tīng)器
Vue.component("account",{
template:"#account",
//組件使用的數(shù)據(jù)
data:function(){
return {
msg:"登錄"
}
},
//組件使用的方法
methods: {
login:function(){
console.log("賬號(hào)")
}
}
})
第三步:使用組件
<account></account>