Answer the question
In order to leave comments, you need to log in
Vuejs reactivity issues?
html
<div id="app" >
<h2>Todo:</h2>
<ol class='tasks' >
<t-element v-for='t in todos' :t='t' ></t-element>
</ol>
<h3>
Add new task:
</h3>
<input v-model="newt" type='text'/>
<button @click="addt" >
Add task
</button>
</div>
Vue.component('t-element',{
props : ['t'],
data : function () {
},
template : `<li>
<label>
<input type="checkbox"
>
<span >
{{ t.text }}:{{t.timeput}}
</span>
</label>
</li>`,
});
new Vue({
el: "#app",
data: {
todos: [
{ text: "t" timeput:20 },
{ text: "tt", timeput:1},
{ text: "ttt", timeput:19 },
{ text: "tttt", timeput:100 }
],
newt : "",
},
mounted (){
this.todos.forEach(function(s){
s.timeput = setInterval(function(){
s.timeput += 1;
}, 1000);
});
},
methods: {
addt: function(){
this.todos.push({
text:this.newt,
timeput:2
});
this.newt = '';
},
}
});
Answer the question
In order to leave comments, you need to log in
the method mounted()
fires only once, when the component is placed.
After adding a new element mounted()
, it is not called and the timer for the added element is not started.
There is no need to create an individual timer for each element. Make one timer for the entire component, update all elements when it fires:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question