export default {
props: {
goToSomePage: {
type: Function,
default: () => { this.$router.push({ name: 'some-page' }) }
}
}
}
我想做这样的事情,但“this”未定义。
有什么办法可以解决这个问题吗?
我想给出一个默认操作并在回调函数中使用“this”。
最佳答案
您可以将箭头函数更改为匿名函数,如下所示
export default {
props: {
goToSomePage: {
type: Function,
default: function(){ this.$router.push({ name: 'some-page' }) }
}
}
}
匿名函数为此使用封闭范围,因此您不必自行绑定(bind)它。这意味着这里的匿名函数将使用与其他选项相同的作用域
关于javascript - 如何在vue组件默认prop函数中使用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587767/