Vue最基础的父子组件例子

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.17.0/axios.min.js"></script>
</head>
<body>
	<!-- 学习父子组件最重要有这个思想:每一个组件都是Vue的实例 -->
	<!-- 里面的组件只能里面调用,如父类组件模板调用子类组件(一层套一层) -->
	<div id="box">
		<parent></parent>		
	</div>

	<!-- 父组件 -->
	<template id="fa">
		<div>
			<h1>父组件</h1>
			<!-- 在组件模板内调用组件(父类组件调用子类组件标签) -->
			<child></child>
		</div>
		
	</template>

	<!-- 子组件 -->
	<template id="ch">
		<div>子组件</div>
	</template>

	<script type="text/javascript">
		var vm=new Vue({
			el:"#box",
			//定义父类组件parent
			components:{
				"parent":{
					template:"#fa",
					/*定义子组件child,子组件只能在父类(parent)内调用,无法单独出现在Vue的作用域内*/
					components:{
						"child":{
							template:"#ch"
						}
					}
				}
			}
		});
	</script>
</body>
</html>

子类组件传值给父组件
父类组件传值给子类组件

猜你喜欢

转载自blog.csdn.net/chijiajing/article/details/82859626
今日推荐