【发布时间】:2019-11-13 15:03:20
【问题描述】:
因此,在按照初学者 Vue 教程设置 Todo 应用程序后,我决定尝试调整其中的某些部分以适应我正在尝试制作的网站。我坚持的是,尽管一切都说我的 for 循环应该工作,但它没有。
项目本身是使用 vue-cli 创建的,大部分代码都是从教程中复制粘贴的。 (使用自己的 for 循环可以正常工作)
似乎数据可能没有传递到模板上?
我试过了:
- 在 props 和 data 部分中包含信息
- 将整个对象和仅参数传递给模板
- 尝试在迭代的数组中使用硬编码值
(设置新的 vue-cli 项目后:)
App.vue:
<template>
<div id="app">
<create-section v-on:create-section="addSection" />
<section v-for="section in sections" v-bind:key="section.title" :info="section"></section>
</div>
</template>
<script>
import CreateSection from "./components/CreateSection";
import Section from "./components/Section";
export default {
name: "App",
components: {
CreateSection,
Section
},
data() {
return {
sections: []
};
},
methods: {
addSection(section) {
this.sections.push({
title: section.title,
description: section.description
});
console.log(
"Added to sections! : " + section.title + " | " + section.description
);
console.log("Sections length: " + this.sections.length);
}
}
};
</script>
Section.vue
<template>
<div class="ui centered card">
<div class="content">
<div class="header">{{ info.title }}</div>
<div>{{ info.description }}</div>
</div>
</div>
</template>
<script type = "text/javascript" >
export default {
props: {info: Object},
data() {
return {};
}
};
</script>
预期结果: 在网站上显示部分模板(在使用另一个脚本调用的 addSection 创建它之后。为简洁起见不包括在内)
实际结果: 什么都不显示,只添加了一个空标签
【问题讨论】:
标签: javascript vue.js vuejs2