vue报错:The template root requires exactly one element

这段话的意思是:根模板必须有一个准确的元素
在vue组件中会使用template标签,在template中,还需要一个标签元素将其他标签元素包裹起来,因为template标签是不会被DOM解析,生成DOM元素的时候会被隐藏,而组件又必须只能有一个根节点

//helloWorld.vue
//正确写法
<template>
	<div>
		<h1>我是h1</h1>
		<h1>我是h1</h1>
		<h1>我是helloWorld</h1>
	</div>
</template>
//错误写法,会报错。
<template>
	<h1>我是h1</h1>
	<h1>我是h1</h1>
	<h1>我是helloWorld</h1>
</template>

这种最外层标签元素只能有一个的方式跟React中的组件用法一致。
React中render函数return组件的时候,最外层也必须只能有一个标签元素,将其他标签严肃包裹起来,否则会报错。

//helloWorld.jsx
render(){
	return(
		<div>
			<h1>我是h1</h1>
			<h1>我是h1</h1>
			<h1>我是helloWorld</h1>
	  	</div>
	)
}
发布了45 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ThisEqualThis/article/details/103196432