admin管理员组文章数量:1023885
When addTodo
is triggered and I inspect this
inside of it, the context is the browser window, not the data
object. So todos
ends up being undefined.
Any idea what I'm missing?
HTML:
<div id="todo-list">
<input type="text" v-model="newTodo">
<button v-on:click="addTodo">Add</button>
<ul>
<li v-if="todos.length" v-for="todo in todos" class="todo-item">
{{ todo }}
</li>
</ul>
</div>
JS:
new Vue({
el: '#todo-list',
data: {
todos: [],
newTodo: ''
},
methods: {
addTodo: () => {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: () => {
this.newTodo = '';
}
}
});
When addTodo
is triggered and I inspect this
inside of it, the context is the browser window, not the data
object. So todos
ends up being undefined.
Any idea what I'm missing?
HTML:
<div id="todo-list">
<input type="text" v-model="newTodo">
<button v-on:click="addTodo">Add</button>
<ul>
<li v-if="todos.length" v-for="todo in todos" class="todo-item">
{{ todo }}
</li>
</ul>
</div>
JS:
new Vue({
el: '#todo-list',
data: {
todos: [],
newTodo: ''
},
methods: {
addTodo: () => {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: () => {
this.newTodo = '';
}
}
});
Share
Improve this question
edited Jul 28, 2016 at 21:23
Bryce Johnson
asked Jul 28, 2016 at 21:17
Bryce JohnsonBryce Johnson
6,9317 gold badges46 silver badges52 bronze badges
3
- Why do you use addTodo: () => { instead of addTodo: function() { } - anyways your code is correct, this.todos should definetly be defined. Try to pare your code with the example: vuejs/examples/svg.html – xate Commented Jul 28, 2016 at 21:28
- @Xatenev I guess I find it shorter and cleaner. It's ES6. But your mentioning that reminded me that ES6 arrow functions don't behave the same as the old function syntax when setting the context (this) and ... changing it to the old syntax fixes the problem. I'm going to write up an answer for why this works in a sec, unless you do. – Bryce Johnson Commented Jul 28, 2016 at 21:37
-
3
() =>
changes the value ofthis
to the enclosing context. In this case, thethis
may be equal towindow
. There's your problem. If you want something concise you can useaddTodos() {}
instead ofaddTodos: function() {}
. – Dan Commented Jul 28, 2016 at 21:44
3 Answers
Reset to default 4It looks like the ES6 arrow syntax is your problem. Change it to use the traditional function() syntax and it will work:
addTodo: function() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: function() {
this.newTodo = '';
}
Quick fix: don't use arrow functions to declare your Vue methods.
What's the problem?
You're expecting the ES6 arrow function () => {}
syntax to set the context (this
) the same as the old function declaration syntax function () {}
would.
Why is that a problem?
From MDN:
Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an "object method", etc.). This proved to be annoying with an object-oriented style of programming.
So, your methods object should look like this (using the old function syntax):
methods: {
addTodo: function() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: function() {
this.newTodo = '';
}
}
Or this (using the new method definition syntax)
methods: {
addTodo() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo() {
this.newTodo = '';
}
}
I don't know a ton about how Vue.js sets/handles context at this point, but it looks like your method is being called from your template/the DOM and the context is being passed from there into your method. Since the arrow function inherits its context, this
refers to the window
object.
Using actual function declarations will preserve a proper reference to the this
you want.
Your arrow Vue methods are already getting the this
object as the first parameter, so:
methods: {
addTodo: (_this) => {
_this.todos.push(_this.newTodo);
_this.clearNewTodo();
},
clearNewTodo: (_this) => {
_this.newTodo = '';
}
}
does the trick, but I am not sure that arrow functions are contributing anything here.
When addTodo
is triggered and I inspect this
inside of it, the context is the browser window, not the data
object. So todos
ends up being undefined.
Any idea what I'm missing?
HTML:
<div id="todo-list">
<input type="text" v-model="newTodo">
<button v-on:click="addTodo">Add</button>
<ul>
<li v-if="todos.length" v-for="todo in todos" class="todo-item">
{{ todo }}
</li>
</ul>
</div>
JS:
new Vue({
el: '#todo-list',
data: {
todos: [],
newTodo: ''
},
methods: {
addTodo: () => {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: () => {
this.newTodo = '';
}
}
});
When addTodo
is triggered and I inspect this
inside of it, the context is the browser window, not the data
object. So todos
ends up being undefined.
Any idea what I'm missing?
HTML:
<div id="todo-list">
<input type="text" v-model="newTodo">
<button v-on:click="addTodo">Add</button>
<ul>
<li v-if="todos.length" v-for="todo in todos" class="todo-item">
{{ todo }}
</li>
</ul>
</div>
JS:
new Vue({
el: '#todo-list',
data: {
todos: [],
newTodo: ''
},
methods: {
addTodo: () => {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: () => {
this.newTodo = '';
}
}
});
Share
Improve this question
edited Jul 28, 2016 at 21:23
Bryce Johnson
asked Jul 28, 2016 at 21:17
Bryce JohnsonBryce Johnson
6,9317 gold badges46 silver badges52 bronze badges
3
- Why do you use addTodo: () => { instead of addTodo: function() { } - anyways your code is correct, this.todos should definetly be defined. Try to pare your code with the example: vuejs/examples/svg.html – xate Commented Jul 28, 2016 at 21:28
- @Xatenev I guess I find it shorter and cleaner. It's ES6. But your mentioning that reminded me that ES6 arrow functions don't behave the same as the old function syntax when setting the context (this) and ... changing it to the old syntax fixes the problem. I'm going to write up an answer for why this works in a sec, unless you do. – Bryce Johnson Commented Jul 28, 2016 at 21:37
-
3
() =>
changes the value ofthis
to the enclosing context. In this case, thethis
may be equal towindow
. There's your problem. If you want something concise you can useaddTodos() {}
instead ofaddTodos: function() {}
. – Dan Commented Jul 28, 2016 at 21:44
3 Answers
Reset to default 4It looks like the ES6 arrow syntax is your problem. Change it to use the traditional function() syntax and it will work:
addTodo: function() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: function() {
this.newTodo = '';
}
Quick fix: don't use arrow functions to declare your Vue methods.
What's the problem?
You're expecting the ES6 arrow function () => {}
syntax to set the context (this
) the same as the old function declaration syntax function () {}
would.
Why is that a problem?
From MDN:
Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an "object method", etc.). This proved to be annoying with an object-oriented style of programming.
So, your methods object should look like this (using the old function syntax):
methods: {
addTodo: function() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo: function() {
this.newTodo = '';
}
}
Or this (using the new method definition syntax)
methods: {
addTodo() {
this.todos.push(this.newTodo);
this.clearNewTodo();
},
clearNewTodo() {
this.newTodo = '';
}
}
I don't know a ton about how Vue.js sets/handles context at this point, but it looks like your method is being called from your template/the DOM and the context is being passed from there into your method. Since the arrow function inherits its context, this
refers to the window
object.
Using actual function declarations will preserve a proper reference to the this
you want.
Your arrow Vue methods are already getting the this
object as the first parameter, so:
methods: {
addTodo: (_this) => {
_this.todos.push(_this.newTodo);
_this.clearNewTodo();
},
clearNewTodo: (_this) => {
_this.newTodo = '';
}
}
does the trick, but I am not sure that arrow functions are contributing anything here.
本文标签: javascriptWhy isn39t my Vue method referring to the proper context (data)Stack Overflow
版权声明:本文标题:javascript - Why isn't my Vue method referring to the proper context (data)? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745527496a2154593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论