带有 v-for 的 Vue 内联模板 - 未定义
Vue inline-template with v-for - not defined
我正在尝试学习 Vue 并尝试实施一个简单的测试,利用内联模板和 v-for
循环。
当我加载以下页面时,我收到以下错误并且没有内容呈现到屏幕上。
[Vue warn]: Property or method "posts" is not defined on the instance
but referenced during render. Make sure to declare reactive data
properties in the data option.
我是 Vue 的初学者,如有任何帮助将不胜感激。
<!DOCTYPE html>
<html>
<head>
<title>Vue Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vue@2.3.4/dist/vue.js"></script>
</head>
<body>
<div id="vue-app" class="container">
<post-list inline-template v-for="(post, index) in posts" :key="post.id">
<div class="post">
<h1>{{ post.subject }}</h1>
</div>
</post-list>
</div>
<script>
Vue.component('post-list', {
data: function() {
return {
posts: [
{ id: 0, subject: 'The first post subject' },
{ id: 1, subject: 'The second post subject' }
]
}
}
});
new Vue({
el: '#vue-app'
});
</script>
</body>
</html>
谢谢。
您的v-for
需要在 模板中。 inline-template 表示元素 内 的所有内容都是模板。
the component will use its inner content as its template
v-for
不在里面,所以它试图迭代 post-list
组件在根 Vue 上寻找 posts
但它不在那里。
此外,迭代根元素会产生多个根(这是不允许的),因此我将 v-for
包装在 div
.
中
Vue.component('post-list', {
data: function() {
return {
posts: [{
id: 0,
subject: 'The first post subject'
},
{
id: 1,
subject: 'The second post subject'
}
]
}
},
});
new Vue({
el: '#vue-app'
});
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<div id="vue-app" class="container">
<post-list inline-template>
<div>
<div class="post" v-for="(post, index) in posts" :key="post.id">
<h1>{{ post.subject }}</h1>
</div>
</div>
</post-list>
</div>
我正在尝试学习 Vue 并尝试实施一个简单的测试,利用内联模板和 v-for
循环。
当我加载以下页面时,我收到以下错误并且没有内容呈现到屏幕上。
[Vue warn]: Property or method "posts" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
我是 Vue 的初学者,如有任何帮助将不胜感激。
<!DOCTYPE html>
<html>
<head>
<title>Vue Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vue@2.3.4/dist/vue.js"></script>
</head>
<body>
<div id="vue-app" class="container">
<post-list inline-template v-for="(post, index) in posts" :key="post.id">
<div class="post">
<h1>{{ post.subject }}</h1>
</div>
</post-list>
</div>
<script>
Vue.component('post-list', {
data: function() {
return {
posts: [
{ id: 0, subject: 'The first post subject' },
{ id: 1, subject: 'The second post subject' }
]
}
}
});
new Vue({
el: '#vue-app'
});
</script>
</body>
</html>
谢谢。
您的v-for
需要在 模板中。 inline-template 表示元素 内 的所有内容都是模板。
the component will use its inner content as its template
v-for
不在里面,所以它试图迭代 post-list
组件在根 Vue 上寻找 posts
但它不在那里。
此外,迭代根元素会产生多个根(这是不允许的),因此我将 v-for
包装在 div
.
Vue.component('post-list', {
data: function() {
return {
posts: [{
id: 0,
subject: 'The first post subject'
},
{
id: 1,
subject: 'The second post subject'
}
]
}
},
});
new Vue({
el: '#vue-app'
});
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<div id="vue-app" class="container">
<post-list inline-template>
<div>
<div class="post" v-for="(post, index) in posts" :key="post.id">
<h1>{{ post.subject }}</h1>
</div>
</div>
</post-list>
</div>