vue.js父子组件

父子组件:

1. 父子组件间的作用域相互独立。

2. 子组件只能在父组件的配置项里创建。

3. 子组件只能在父组件的模板中进行调用。

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
	<script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
	<div id="div">
		<parent></parent>
		<son></son>
	</div>
	
	<template id="parent">
		<div>
			<p>{{ msg }}{{ msg2 }}</p>
			<!--调用子组件-->
			<son></son>
		</div>
		
	</template>
	
	<template id="son">
		<p>{{ msg2 }}{{ msg }}</p>
	</template>
</body>
</html>
<script type="text/javascript">
	var vm = new Vue({
		el: '#div',
		components:{
			'parent':{
				template:'#parent',
				data:function(){
					return{
						'msg':'haha'
					}
				},				
//				创建子组件
				components:{
					'son':{
						template:'#son',
						data:function(){
							return{
								'msg2':'hehe'
							}
						}
					}
				}
			}
			
		}
	})
</script>

猜你喜欢

转载自blog.csdn.net/qq_39383675/article/details/83583446