VUE使用axios数据请求时报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
正常定义全局变量:
data:function (){
return{
currentOrders:[]
}
},
使用Axios发送请求并获取后端数据时,如果在then中使用this.currentOrders会出现TypeError: Cannot set property 'xxxx' of undefined错误。
原因是使用this$axios().then(function(response){}).catch()格式获取参数,then的内部 this 没有被绑定,所以不能使用Vue的实例化的this。
解决办法一:在this.$axios外部将this赋值为自定义变量:var that = this,然后在then里面使用that.currentOrders
var that = this
this.$axios({
method: 'get',
url: 'xxxxxx',
}).then(function (response) {
var jsonObj = JSON.parse(JSON.stringify(response.data))
if (jsonObj.code === ERR_ok){
that.currentOrders = jsonObj.data
}
}).catch(function (error) {
console.log(error);
})
第二种方法:用ES6箭头函数,箭头方法可以和父方法共享变量
this.$axios({
method: 'get',
url: 'xxxxxx',
}).then((response)=>{})
.catch(){}
VUE使用axios数据请求时报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法的更多相关文章
- VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...
- VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
created() { var that=this axios.get('http://jsonplaceholder.typicode.com/todos') .then( ...
- 使用webpack命令打包时,报错TypeError: Cannot read property 'presetToOptions' of undefined的解决办法
我只安装了webpack,没有安装webpack-cli,第一次输入webpack打包时,提示 One CLI for webpack must be installed. These are rec ...
- vue表单校验提交报错TypeError: Cannot read property 'validate' of undefined
TypeError: Cannot read property 'validate' of undefined at VueComponent.submitForm (plat_users.html: ...
- Node中使用MySQL报错:TypeError: Cannot read property 'query' of undefined
Node中使用MySQL报错: TypeError: Cannot read property 'query' of undefined at /Users/sipeng/Desktop/彭思/201 ...
- vue报错TypeError: Cannot read property 'protocol' of undefined
错误信息如下所示: isURLSameOrigin.js?3934:57 Uncaught (in promise) TypeError: Cannot read property 'protocol ...
- Vue使用Typescript开发编译时提示“ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined”的解决方法
使用Typescript开发Vue,一切准备就绪.但npm start 时,提示“ ERROR in ./src/main.tsModule build failed: TypeError: Cann ...
- day 74 vue 2 axios数据请求 以及组件的学习
前情提要: vue 学习二: 一: 通过axios实现数据请求 1:json数据语法 json数据对象类似于JavaScript中的对象,但是它的键对应的值里面是没有函数方法的,值可以是普通变量, ...
- vue 使用axios 数据请求第三方插件的使用
axios 基于http客户端的promise,面向浏览器和nodejs 特色 浏览器端发起XMLHttpRequests请求 node端发起http请求 支持Promise API 监听请求和返回 ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
随机推荐
- 网络连接(CSP-J 2021 T3)
一道又臭又长的模拟题目..... 主要有几个小技巧可以帮大家省一省代码: 1.scanf("%d.%d",&b,&c); 这里是定了一个格式如果不按格式来就不输入后 ...
- 洛谷p1423
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 double n;//n:要游的距离 6 cin&g ...
- vivo官网App模块化开发方案-ModularDevTool
作者:vivo 互联网客户端团队- Wang Zhenyu 本文主要讲述了Android客户端模块化开发的痛点及解决方案,详细讲解了方案的实现思路和具体实现方法. 说明:本工具基于vivo互联网客户端 ...
- C#获取html标签内容的方法
C# 获取html标签内容的方法: /// <summary> /// 获取html网页标签内容 /// 例如:<span class="index_infoItem__E ...
- 视觉十四讲:第九讲_BA优化_g2o
1.投影模型和BA代价函数 这个流程就是观测方程 之前抽象的记为: \(z = h(x, y)\) 现在给出具体的参数话过程,x指此时相机的位姿R,t,它对应的李代数为\(\xi\).路标y即为这里的 ...
- CF1141F2 Same Sum Blocks (Hard)
题目传送门 思路 简单题. 不妨先预处理出每一个区间的 \(\sum\),然后离散化 \(\sum\),对于每个 \(\sum\) 开一个 \(\mathcal vector\) 记录所有区间的左右端 ...
- LeetCode-442 数组中重复的数据
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/find-all-duplicates-in-an-array 题目描述 给你一个长度为 n 的整 ...
- autodesk2023全家桶 Autodesk 2023 所有产品下载地址
autodesk2023全家桶 Autodesk 2023 所有产品下载地址 作者: 佚名 2022/4/10 11:51:24 Autodesk系列软件每年都会提前发布,目前autodesk2023 ...
- RESTful风格与Spring注解
RESTfulL是一种网络应用程序的设计风格和开发方式,即接口请求方式和路径的一种风格. 普通风格: localhost:8080/add?a=1&b=2 RestFul风格: localho ...
- 野火FreeRTOS计数信号量实验意外处理
编译的时候,一直说xSemaphoreCreateCounting这个函数没有定义. 最后发现,是FreeRTOSConfig.h文件中,没有将使能计数信号量的宏打开. 解决办法:在FreeRTOSC ...