vue组件初步认识

1 组件的作用

  • 组件(Component)是 Vue.js 最强大的功能之一,在组件内可以实现扩展html的元素,封装重复使用的代码。

2 全局注册与局部注册

  • 二者的区别,全局注册的组件可以实现所有创建vue实例调用,而局部注册只能使用一个。
  • 全局注册
<body>
		<div id="app">
			<my-component></my-component>
		</div>
		<script type="text/javascript">
			Vue.component("my-component",{
				template:'<div>一个自定义组件</div>'
			})
			
			new Vue({
				el:"#app"
			})
		</script>
	</body>
  • 全局注册
<body>
		<div id="app">
			<my-child></my-child>
		</div>
		
		<script type="text/javascript">
			var child={
				template:'<div>一个自定义组件</div>'
			}
			
			new Vue({
				el:"#app",
				components:{
					'my-child':child
				}
			})
			
			
			
		</script>
	</body>
  • data 必须是一个函数
<body>
		<div id="app">
			<counter></counter>
			<counter></counter>
			<counter></counter>
		</div>
		<script type="text/javascript">
			var counter={
				template:`<button @click="num++">{{num}}</button>`,
				data:function(){
					return {num:1}
				}
			}
			new Vue({
				el:"#app",
				components:{
					counter,
				}
			})
			
			
		</script>
	</body>

3 组件传参

  • 当一个值传递给一个 prop 特性的时候,它就变成了那个组件实例的一个属性。
<body>
		<div id="app">
			<child message="hello!"></child>
		</div>
		<script type="text/javascript">
			Vue.component('child', {
				props: ['message'],
				template: '<span>{{ message }}</span>'
			})
			new Vue({
				el:"#app"
			})
		</script>
	</body>
  • 实现动态传参,通过实现template内部运行是pros的值实现。
<body>
		<div id="app">
           <blog-post title="My journey with Vue"></blog-post>
           <blog-post title="Blogging with Vue"></blog-post>
           <blog-post title="Why Vue is so fun"></blog-post>
		</div>
		
		<blog-post title="My journey with Vue"></blog-post>
		<blog-post title="Blogging with Vue"></blog-post>
		<blog-post title="Why Vue is so fun"></blog-post>

		<script type="text/javascript">
			
			Vue.component('blog-post', {
				props: ['title'],
				template: '<h3>{{ title }}</h3>'
			})
			
			new Vue({
				el:"#app"
			})
			
		</script>
	</body>

4 子组件怎么跟父组件通信

我们知道,父组件使用 prop 传递数据给子组件。但子组件怎么跟父组件通信呢?这个时候 Vue 的自定义事件系统就派得上用场了。
每个 Vue 实例都实现了事件接口,即:
使用 $on(eventName) 监听事件
使用 $emit(eventName) 触发事件
父组件可以在使用子组件的地方直接用 v-on 来监听子组件触发的事件。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<p>{{ total }}</p>
			<counter v-on:add="addTotal"></counter>
			<counter v-on:add="iaddTotal"></counter>
		</div>


		<script type="text/javascript">
			Vue.component('counter', {
				template: '<button v-on:click="addCounter">{{ counter }}</button>',
				data: function() {
					return {
						counter: 0
					}
				},
				methods: {
					addCounter: function() {
						this.counter += 1
						this.$emit('add')
					}
				},
			})
			new Vue({
				el: '#app',
				data: {
					total: 0
				},
				methods: {
					addTotal: function() {
						this.total += 1
					}
				}
			})
		</script>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40994735/article/details/108304090