【发布时间】:2019-11-19 15:38:49
【问题描述】:
在尝试声明与解构结合的默认参数时,我在使用 this 引用 data 时遇到问题。有人知道怎么做吗?
activated () {
this.fillData()
},
data () {
return {
chartData: {
distributionInitialData: {
// data here
}
}
}
},
methods: {
fillData ({
must = this.chartData.distributionInitialData,
nice = this.chartData.distributionInitialData
}) {
// do something
// function doesn't even get here because it gets error:
// TypeError: Cannot read property 'must' of undefined
// at VueComponent.fillData (Component.vue?4e6a:230)
// at VueComponent.activated (Component.vue?4e6a:123)
}
}
【问题讨论】:
-
不确定是否重复但绝对相关:stackoverflow.com/a/58934526/1048572
-
我的问题更多的是无法访问
this。所有其他的东西都在工作。现在我不得不解决两个论点而不是解构一个论点,但问题仍然有效 - 我认为这是一个非常有趣的问题要解决。 -
访问
this工作正常。问题是您分配这些属性,而不是使用它们作为默认值。 -
我尝试使用
=,但因为它是 JSON,我不得不改用:,这就是它爆炸的地方。如果我使用=,则会收到undefined错误。 -
周围没有 JSON。这是方法定义中的一种解构模式。您能否发布整个代码(方法主体,以及您如何调用它)以及您收到的完整错误消息?
标签: javascript vue.js