无法访问监视处理程序vuejs中的数据变量

Wil*_*lah 2 javascript vue.js vue-component vuejs2

我正在尝试在VueJs组件中的输入字段的监视处理函数中设置数据变量.我有这样的事情:

data() {
    return {
        params: {
            // default params to 1 month
            from: new Date().setMonth(new Date().getMonth() - 1),
            to: Date.now(),
            skip: 0,
            limit: 100
        }
    }
}
watch: {
    dates: { 
        handler: date => {
            console.log(this.params)
            if (date.start) {
                this.params.from = moment(date.start, "YYYY/MM/DD")
            }
            if (date.end) {
                this.params.to = moment(date.end, "YYYY/MM/DD")
            }
        },
        deep: true
    }
}
Run Code Online (Sandbox Code Playgroud)

当我dates在视图模板中为变量设置输入时,我在控制台日志中得到一个undefinedfor this.params,并且尝试设置时出错this.params.from.所以我尝试使用一种方法访问它:

methods: {
    printParams() {
        console.log(this.params)
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图模板中调用它,它正确地解析了params对象.

我在这里错过了什么吗?

Bel*_*dak 20

为了避免额外的绑定,请避免在此使用箭头函数语法.而不是使用ES6对象缩写:

watch: {
    dates: { 
        handler(date) {
            console.log(this.params)
            if (date.start) {
                this.params.from = moment(date.start, "YYYY/MM/DD")
            }
            if (date.end) {
                this.params.to = moment(date.end, "YYYY/MM/DD")
            }
        },
        deep: true
    }
}
Run Code Online (Sandbox Code Playgroud)

现在this默认情况下将绑定到正确的上下文.