全局注冊
main.js中創建
Vue.component('button-counter', {
data: function () {
return {
count: 0
}
},
template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})
使用
<div id="components-demo">
<button-counter></button-counter>
</div>
new Vue({ el: '#components-demo' })
局部注冊
直接在.vue文件中使用
第一種方式
通過一個普通的 JavaScript 對象來定義組件:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
然后在 components 選項中定義你想要使用的組件:
new Vue({
el: '#app',
components: {
'component-a': ComponentA,
'component-b': ComponentB
}
})
如果你希望 ComponentA 在 ComponentB 中可用,則你需要這樣寫:
var ComponentA = { /* ... */ }
var ComponentB = {
components: {
'component-a': ComponentA
},
// ...
}
第二種方式
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA //ComponentA:ComponentA
},
// ...
}