我试图在watch handler函数中为VueJs组件中的输入字段设置一个数据变量.我有这样的 idea :
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
}
}
当我在视图模板中为dates
变量设置输入时,控制台日志(log)中的this.params
为undefined
,而我在try 设置this.params.from
时出错.所以我试着用一种方法访问它:
methods: {
printParams() {
console.log(this.params)
}
}
在视图模板中调用它,它会正确解析params
对象.
我错过什么了吗?