countSell(){ this.$http("http://localhost:9090/count").then(res=>{ this.countData=res.data.data; console.log(this.countData[1].money) }); console.log(this.countData[1].money) },为什么 两个输出不一样
时间: 2023-06-19 13:04:55 浏览: 38
这是因为 `this.$http` 是一个异步请求,会在后台发送请求,并不会阻止后续代码的执行。因此,在第一个 `console.log` 执行时,`this.countData` 尚未被赋值,因此输出的是 `undefined`。而在异步请求完成后,`this.countData` 被赋值为请求返回的数据,因此在第二个 `console.log` 执行时,能够输出正确的数据。
如果你想要在 `countSell` 中正确输出 `this.countData[1].money`,可以将第二个 `console.log` 放在异步请求的回调函数中,这样就能够保证 `this.countData` 已经被赋值了。例如:
```
countSell() {
this.$http("http://localhost:9090/count").then(res => {
this.countData = res.data.data;
console.log(this.countData[1].money);
console.log(this.countData);
});
},
```
这样第二个 `console.log` 就能正确输出 `this.countData` 的值了。
相关问题
goBack() { this.$router.push("/manageEvent") }, getEvent(){ let _this = this axios.get('http://localhost:8082/event/getEvent?name=' +_this.$route.query.name) .then(function (ref) { _this.eventInformation = ref.data.data[0] console.log(_this.eventInformation.venueId) _this.imageUrl = `http://localhost:8082/common/download?name=${_this.eventInformation.picture}` axios.get('http://localhost:8082/venue/getVenue?id=' +_this.eventInformation.venueId) .then(function (ref) { console.log(ref.data) _this.venue = ref.data.data.name _this.university = ref.data.data.university }) }) },
这段代码是关于前端页面中获取活动信息和场馆信息的部分代码。其中,getEvent()函数使用了axios库向后端发送请求,获取活动信息和对应的场馆信息,并将它们赋值给前端页面中的变量。具体来说,它通过调用axios.get()方法发送GET请求,其中请求的URL包括了活动名称和场馆ID。然后,使用.then()方法对请求的结果进行处理,在回调函数中将返回的活动信息和场馆信息赋值给前端页面中的变量,并打印出场馆ID和场馆名称。最后,获取到的活动图片URL会赋值给imageUrl变量。另外,goBack()函数则是实现了路由的跳转,将页面跳转到“/manageEvent”路由下。
根据这段代码写一个删除用户的方法updateContent(){ axios.post('http://localhost:8082/users/update',{ userId:this.changeTable.userId, userName:this.changeTable.userName, userSex:this.changeTable.userSex, userAccount:this.changeTable.userAccount, userPassword:this.changeTable.userPassword, userIdentity:this.changeTable.userIdentity, userIntroduction:this.changeTable.userIntroduction, userEmail:this.changeTable.userEmail, userPortrait:this.changeTable.userPortrait, }) .then(response=>{ this.$message.success("修改成功!") this.dialogFormVisible=false axios.get(`http://localhost:8082/users/${this.currentPage}/${this.pageSize}`) .then(response=>{ this.tableData=response.data.data.records this.total=response.data.data.total this.currentPage=response.data.data.current }).catch((error)=> { console.log(error); }) }).catch((error)=> { console.log(error) }) },
根据你提供的代码,我们需要编写一个删除用户的方法,可以参考以下代码:
```javascript
deleteUser() {
axios.delete(`http://localhost:8082/users/${this.changeTable.userId}`)
.then(response => {
this.$message.success("删除成功!")
this.dialogFormVisible = false
axios.get(`http://localhost:8082/users/${this.currentPage}/${this.pageSize}`)
.then(response => {
this.tableData = response.data.data.records
this.total = response.data.data.total
this.currentPage = response.data.data.current
}).catch((error) => {
console.log(error);
})
}).catch((error) => {
console.log(error)
})
},
```
这里我们使用了axios的delete方法,将要删除的用户的id作为参数传递给后端,删除成功后再重新获取用户列表进行渲染。在调用该方法时,需要保证this.changeTable.userId有值。