getResult(){ let requestParam1 = {} let requestParam2 = {} requestParam1.screen = this.screenData; requestParam1.dimension = [this.dimensionData[0]]; requestParam1.measure = this.measureData; requestParam1.formInline = this.formInline || {}; requestParam1.tableSel = this.tableSel; requestParam1.chartType = "scatter"; requestParam2.screen = this.screenData; requestParam2.dimension = [this.dimensionData[1]]; requestParam2.measure = this.measureData; requestParam2.formInline = this.formInline || {}; requestParam2.tableSel = this.tableSel; requestParam2.chartType = "scatter"; this.getChartDataLater(requestParam1); this.getChartDataLater(requestParam2); let _options = JSON.parse(JSON.stringify(this.chartOptions)); let _temp_options = scatterRender(this.dimensionData, this.measureData, _options, this.results); if(_temp_options){ this.chartOptions = _temp_options; } }, getChartDataLater(val) { getChartData(val).then(res => { this.results.push(res); }).catch(err => { console.log(err); }); }
- 上面的这种写法可能会出现,getChartDataLater方法还没执行完成,this.results这个数组还没有push两次(可能ajax异步导致),就执行了下面得到_options的代码,这时候参数中this.results还只是个空数组
getResult(){ let requestParam1 = {} let requestParam2 = {} requestParam1.screen = this.screenData; requestParam1.dimension = [this.dimensionData[0]]; requestParam1.measure = this.measureData; requestParam1.formInline = this.formInline || {}; requestParam1.tableSel = this.tableSel; requestParam1.chartType = "scatter"; requestParam2.screen = this.screenData; requestParam2.dimension = [this.dimensionData[1]]; requestParam2.measure = this.measureData; requestParam2.formInline = this.formInline || {}; requestParam2.tableSel = this.tableSel; requestParam2.chartType = "scatter"; this.getChartDataLater(requestParam1); this.getChartDataLater(requestParam2); }; getChartDataLater(val) { getChartData(val).then(res => { this.results.push(res); this.a_watch = this.a_watch + 1 }).catch(err => { console.log(err); }); } watch : { a_watch(newval, oldVal) { console.log(newval, oldVal) if(newval == 2){ console.log(this.results) let _options = JSON.parse(JSON.stringify(this.chartOptions)); let _temp_options = scatterRender(this.dimensionData, this.measureData, _options, this.results); if(_temp_options){ this.chartOptions = _temp_options; } } } }
1.watch监听data内数据的变化
data: { a: 100 }, watch: { a(newval, oldVal) { console.log(newval, oldVal) } }
2.watch监听整个对象,deep: true 深度监测
data: { return { msg: { name: 'hahah', color: 'red' } } } watch: { msg: { handler(newValue, oldValue) { console.log(newValue) }, deep: true }
3.watch监听对象内的某一具体属性,可以通过computed做中间层来实现
computed: { name() { return this.msg.name } }, watch:{ name(newValue, oldValue) { console.log(newval, oldVal) } }