vue中报错: Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
今天写练习的时候遇到一个问题:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<test></test>
</div>
<script>
Vue.component('test',{
template:`
<p>卢本伟牛逼</p>
<p>干死黄旭东</p>
`
})
var vm = new Vue({
el:'#app'
})
</script>
</body>
</html>
原因是因为,component是要当作一个标签使用的,所以一个component对应一个唯一的根元素,像下面这样,用一个div标签包裹两个p标签就好了。
Vue.component('test',{
template:`
<div>
<p>卢本伟牛逼</p>
<p>干死黄旭东</p>
</div>
`
})