全局组件

一 概念

```html
<div id="app">
    <global-tag></global-tag>
    <global-tag></global-tag>
</div>
<script>
	Vue.component('global-tag', {
		data () {
			return {
				count: 0
			}
		},
		template: '<button @click="btnAction">全局{{ count }}</button>',
		methods: {
			btnAction () {
				this.count ++
			}
		}
	})
    new Vue({
        el: "#app"
    })
</script>
```

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>全局组件</title>
</head>
<body>
<div id="app">
<global-tag v-for="(o, i) in ls" :key="i"></global-tag>
</div>
</body>
<script src="js/vue-2.5.17.js"></script>
<script type="text/javascript">
// 全局组件
// 用Vue.component("组件名", {})来创建全局组件
// 全局组件附属于Vue实例,可以不需要注册就可以使用
Vue.component("global-tag", {
template: "<button @click='btnClick'>{{ n }}</button>",
data () {
return {
n: 0
}
},
methods: {
btnClick () {
this.n++
}
}
})

new Vue({
el: "#app",
data: {
ls: [0, 0, 0]
}
})
</script>
</html>

posted @ 2018-10-30 15:27  不沉之月  阅读(156)  评论(0编辑  收藏  举报