【发布时间】:2018-03-14 13:11:51
【问题描述】:
如何通过 subInput(in component, 2nd input) 发布到组件外的列表中?我可以看到输入发布到 subTaskList,但不会发布到列表?
澄清:我正在尝试制作一个食物计划器,其中 placeholder="Tilføj ret til madplanen" 的主要输入是当天的食物,而 placeholder="Tilføj til indkøbsseddel" 的第二个输入是添加到购物清单 (#list)
(对不起丹麦占位符)
Vue.component('list-component', {
data: function() {
return {
newTask: "",
taskList: [],
newSubTask: "",
subTaskList: [],
};
},
template:
'<div>' +
'<section class="prefetch" class="panel">' +
'<input v-if="showInput" class="input typeahead" type="text" placeholder="Tilføj ret til madplanen" v-model="newTask" v-on:keyup.enter="addTask">' +
'</section>' +
'<details v-for="task in taskList" v-bind:key="task.text" class="sub-list-item">' +
'<summary>{{ task.text }}<button v-on:click="removeSubTask(task)">X</button>' + '</summary>' +
'<input class="subInput" type="text" placeholder="Tilføj til indkøbsseddel" v-model="newSubTask" v-on:keyup.enter="addSubTask">' +
'</details>' +
'</div>',
computed: {
showInput: function() {
return !this.taskList.length
},
},
methods: {
//addTasks
//
addTask: function() {
var task = this.newTask.trim();
if (task) {
this.taskList.push({
text: task
});
this.newTask = "";
}
},
addSubTask: function() {
var task = this.newSubTask.trim();
if (task) {
this.subTaskList.push({
text: task
});
this.newSubTask = "";
}
},
//removeTasks
//
removeSubTask: function(task) {
var index = this.taskList.indexOf(task);
this.taskList.splice(index, 1);
var index = this.subTaskList.indexOf(task);
this.subTaskList.splice(index, 999);
},
},
});
new Vue({
el: "#madplan",
data: {
newTask: "",
taskList: [],
newSubTask: "",
subTaskList: [],
},
});
* {
margin: 0;
padding: 0;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<section id="madplan" class="section-wrapper">
<section class="check-list">
<h1>Mandag</h1>
<list-component></list-component>
<h1>Tirsdag</h1>
<list-component></list-component>
</section>
<ul id="list">
<h2>list</h2>
<li v-for="task in subTaskList" v-bind:key="task.text" class="list-item">{{ task.text }}</li>
</ul>
</section>
【问题讨论】:
标签: vue.js vue-component