Vue.js 创建父、子组件

❗️
在父组件中注册子组件后,在父组件模板template中使用子组件时,应该把父组件模板内容和子组件标签放在同一个HTML标签内,否则会报错无法显示:

<div>
父组件内容
<子组件></子组件>
</div>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Vue.js 创建父组件和子组件</title>
		<script src="vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id='app'>
			<parent-component></parent-component>
		</div>

		<script type="text/javascript">
			//Step 1: 创建子组件
			var ChildComponent = Vue.extend({
				template: '<p>the child comonent.</p>'
			})

			//Step 2: 创建父组件,并且注册子组件
			var ParentComponent = Vue.extend({
				template: '<div><p>the parent component. </p><child-component></child-component></div>',
				components: {
					'child-component': ChildComponent
				}
			})

			//Step 3: 全局注册父组件,子组件也会同时被渲染出来
			Vue.component('parent-component', ParentComponent)
			new Vue({
				el: '#app'
			})
		</script>
	</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值