我对vue分配有疑问。 我的代码是这样的
new Vue({
el: "#alarmEchartBar",
data: {
regusterUrl: Ohttp + "historicalAlarmRecord/chart",
regDistrictUrl: Ohttp + "district",
regStreetUrl: Ohttp + "street/",
regCameraUrl: Ohttp + "camera/",
meger: false,
timeUnit: 1,
startTime: 0,
endTime: 0,
districtName: "全部",
streetName: "全部",
cameraName: "全部",
districtData: [],
streetData: [],
cameraData: [],
DID: [],
SID: [],
CID: [],
requestData: {
"dIds": this.DID,
"cIds": this.CID,
"sIds": this.SID,
"merge": this.meger,
"timeUnit": this.timeUnit,
"startTime": this.startTime,
"endTime": this.endTime,
},
}})
如果分配此值,则requestData是未定义的。
答案 0 :(得分:1)
在这种情况下,最好使用计算属性。
https://vuejs.org/v2/guide/computed.html
请尝试以下代码:
new Vue({
el: "#alarmEchartBar",
data: {
regusterUrl: Ohttp + "historicalAlarmRecord/chart",
regDistrictUrl: Ohttp + "district",
regStreetUrl: Ohttp + "street/",
regCameraUrl: Ohttp + "camera/",
meger: false,
timeUnit: 1,
startTime: 0,
endTime: 0,
districtName: "全部",
streetName: "全部",
cameraName: "全部",
districtData: [],
streetData: [],
cameraData: [],
DID: [],
SID: [],
CID: [],
},
computed: {
requestData: function() {
return {
"dIds": this.DID,
"cIds": this.CID,
"sIds": this.SID,
"merge": this.meger,
"timeUnit": this.timeUnit,
"startTime": this.startTime,
"endTime": this.endTime,
},
}
}
})