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 ...
随机推荐
- JAVA虚拟机20-基于栈的解释器执行过程示例
1.准备代码 public int calc() { int a = 100; int b = 200; int c = 300; return (a + b) * c; } 2.使用javap -v ...
- activiti03 SSM使用activity
1.添加依赖 <!--activity依赖--> <dependency> <groupId>org.activiti</groupId> <ar ...
- Snipaste下载安装(使用教程)
## Snipaste下载安装(使用教程)**一 简单介绍** Snipaste 是一个免费简单但强大的截图工具,也可以让你将截图贴回到屏幕上!下载并打开 Snipaste,按下 F1 来开始截图,再 ...
- sqlserver数据库批量新增修改类
MSSql Server 数据库批量操作 需要引用的命名空间 using System; using System.Collections.Generic; using System.Data; us ...
- 安装kali2021.1系统
基本安装 下载地址:Downloads | Kali Linux 去官网下载,会得到kali2021.1的镜像和哈希值 打开VMware软件,新建虚拟机........................ ...
- dvwa靶场
brute force LOW 输入账号密码直接抓包就行 接着ctrl+i传输到intruder模块中 为需要爆破的加上 §,不爆破的不加 选择攻击类型为Cluster bomb,选择username ...
- go 神奇的错误 time.Now().Format("2006-01-02 13:04:05") 比北京时间大8小时
困倦的时候写了个个获取本地时间,打印总比当前时间大8小时,找了很久原因 package main import ( "fmt" "time" ) func ma ...
- KMP字符串 AcWing 831
题目:https://www.acwing.com/problem/content/833/ 题意:求子串在母串中每次出现时的下标位置. 题解:哈哈哈,敲题时想到之前看到一个人叫 kmp 算法为 看毛 ...
- day11_多态&抽象类&接口
1.多态 1.1 多态的概述(记忆) 什么是多态 同一对象,在不同时刻表现出来的不同形态. 多态的前提 有继承/实现关系 有方法重写 有父类对象的引用执行子类对象 1.2 多态中的成员访问特点 ...
- javaSE学习一
java基础 java是一种强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用. java的数据类型有两大类:基本数据类型和引用类型(类.接口.数组) 八大基本数据类型: 1.整 ...