Vue.js 中組件的注冊
組件介紹:
組件(Component),主要體現(xiàn)了封裝的思想,在較高層面上,組件是自定義元素
使用組建的步驟
全局注冊
在使用Vue時(shí)需要?jiǎng)?chuàng)建一個(gè)全局的Vue對(duì)象.類似的,可以使用Vue.component(tagName, options) .例如
Vue.compponent('firstcomponent', {
// 選項(xiàng)
})
在組件注冊之后,可以在Vue代理的區(qū)域,自定義一對(duì)元素<firstcomponent></firstcomponent>,然后在定義的全局組件中可以寫元素
Vue.compponent('firstcomponent', {
template: '<div>組件</div>'
})
//創(chuàng)建根實(shí)例
new Vue({
el: '#test'
})
渲染結(jié)果為
<div id = test>
<div>組件</div>
</test>
局部注冊
var example = {
template: '<div>組件</div>'
}
new Vue({
// ...
components: {
'my-component': Child
}
})