问题
How can i post through subInput(in component, 2nd input) to the list outside the component? I can see that the input posts to subTaskList, but wont post it to the list?
For clarifications: I'm trying to make a food planner, where the main inputs with placeholder="Tilføj ret til madplanen" to be the food for the day, and the second input with placeholder="Tilføj til indkøbsseddel" to add to a shoppinglist (#list)
(sorry for the danish placeholders)
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>
Fiddle: https://jsfiddle.net/txh85nq0/1/
回答1:
To accomplish communication between Vue.js components, you need to leverage the Custom events
For your components to work as intended you need to make a few modifications.
First correct this line
<section class="prefetch" class="panel">
to remove duplicate class definition. You should use
<section class="prefetch panel">
Then in the method addTask in list-component declaration add the line
this.$emit('addedtask', task);
right after
this.newTask = "";
While at it, why not also add this
this.$emit('removedtask', task);
right after the line
this.subTaskList.splice(index, 999);
in the method removeSubTask on the same list-component declaration
Now catch the events emitted in the child component update #madplan template by changing
<list-component></list-component>
to this
<list-component
v-on:addedtask='acknowledgeAddedTask'
v-on:removedtask='acknowledgeRemovedTask'
></list-component>
You will also need to declare the two new methods so that #madplan now includes
methods: {
acknowledgeAddedTask: function(task) {
this.$data.subTaskList.push({ text: task })
},
acknowledgeRemovedTask: function(task) {
this.$data.subTaskList = this.$data.subTaskList.filter(it => it.text != task.text)
}
}
See updated fiddle
来源:https://stackoverflow.com/questions/46542883/post-to-list-from-vue-component