<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of page</title>
</head>
<body>
<div id="example">
<my-component></my-component>
</div>
<div id="example-2">
<simple-counter></simple-counter>
<simple-counter></simple-counter>
<simple-counter></simple-counter>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
<script >
var Child = {
template: ‘<div>A custom component!</div>‘
}
// 注册
// Vue.component(‘my-component‘, {
// template: Child
// })
// 创建根实例
new Vue({
el: ‘#example‘,
components: {
// <my-component> 将只在父组件模板中可用
‘my-component‘: Child
}
})
var data = { counter: 0 }
Vue.component(‘simple-counter‘, {
template: ‘<button v-on:click="counter += 1">{{ counter }}</button>‘,
// 技术上 data 的确是一个函数了,因此 Vue 不会警告,
// 但是我们却给每个组件实例返回了同一个对象的引用
data: function () {
return {
counter: 0
}
}
})
new Vue({
el: ‘#example-2‘
})
</script>
</html>