TypeError: Cannot set properties of undefined (setting ‘xx‘)的問(wèn)題及解決方法
TypeError: Cannot set properties of undefined (setting ‘xx‘)
問(wèn)題描述
我在編寫(xiě)axios請(qǐng)求后端數(shù)據(jù)的時(shí)候,一直出現(xiàn)下面的錯(cuò)誤前端頁(yè)面顯示不出來(lái)。
報(bào)錯(cuò):
TypeError: Cannot set properties of undefined (setting ‘xx’)
原因分析:
this指向的對(duì)象發(fā)生了變化(現(xiàn)在this代表axios對(duì)象),需要在函數(shù)前將this指向的對(duì)象提前保存一下
解決方案:
方法一:回調(diào)函數(shù)使用箭頭函數(shù)來(lái)使用。(responde)=>{}
<template> <el-table :data="tableData" style="width: 100%" stripe> <el-table-column prop="name" label="姓名" width="250" /> <el-table-column prop="age" label="年齡" width="250" /> <el-table-column prop="gender" label="性別" width="250" /> <el-table-column prop="createTime" label="創(chuàng)建時(shí)間" width="250" /> <el-table-column fixed="right" label="操作" width="250"> <template #default> <el-button link type="primary" size="small">編輯</el-button> <el-button link type="primary" size="small">刪除</el-button> </template> </el-table-column> </el-table> </template> <script> import axios from 'axios'; export default { data() { return { tableData:[] } }, created(){//為何 要在created 方法中 發(fā)請(qǐng)求,因?yàn)樵摲椒?可以操作tableData屬性 // 解決方法一 axios.get('http://localhost:9090/student') // url 請(qǐng)求后臺(tái)的地址 /* * .then(function (response) 這樣寫(xiě)前端會(huì)報(bào)錯(cuò) * 報(bào)錯(cuò)信息:TypeError: Cannot set properties of undefined (setting 'tableData')at eval (Student.vue?401d:59:1) * 這樣寫(xiě)是匿名函數(shù),無(wú)法取到tableData的值,所以tableData的值為undefined,不能給undefined的變量賦值 * 解決辦法:1.改為箭頭函數(shù) 2.將this重新賦值給新的變量 */ // .then(function (response) { //成功回調(diào)方法(訪(fǎng)問(wèn)后臺(tái)成功,后臺(tái)有數(shù)據(jù)返回,則進(jìn)入該方法) .then(response=> { //成功回調(diào)方法(訪(fǎng)問(wèn)后臺(tái)成功,后臺(tái)有數(shù)據(jù)返回,則進(jìn)入該方法) console.log(response); console.log(response.data) this.tableData = response.data; }) .catch(function (error) {//失敗回調(diào)方法(訪(fǎng)問(wèn)后臺(tái)失敗,返回失敗的信息,則進(jìn)入該方法) console.log(error); }); }, methods:{ } } </script>
方法二:暫存this。var th = this; 在內(nèi)部用th.xx代替this.xx
//解決辦法二 var th = this; axios.get('http://localhost:9090/student') // url 請(qǐng)求后臺(tái)的地址 .then(function (response) { //成功回調(diào)方法(訪(fǎng)問(wèn)后臺(tái)成功,后臺(tái)有數(shù)據(jù)返回,則進(jìn)入該方法) console.log(response); console.log(response.data) th.tableData = response.data; }) .catch(function (error) {//失敗回調(diào)方法(訪(fǎng)問(wèn)后臺(tái)失敗,返回失敗的信息,則進(jìn)入該方法) console.log(error); });
到此這篇關(guān)于TypeError: Cannot set properties of undefined (setting ‘xx‘)的文章就介紹到這了,更多相關(guān)Cannot set properties of undefined內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js控制臺(tái)報(bào)錯(cuò)Uncaught TypeError: Cannot read properties of undefined (reading ‘a(chǎn)ppendChild‘)的解決
- React/vue開(kāi)發(fā)報(bào)錯(cuò)TypeError:this.getOptions?is?not?a?function的解決
- 解決TypeError: Object of type xxx is not JSON serializable錯(cuò)誤問(wèn)題
- 后端報(bào)TypeError:Cannot?read?properties?of?null?(reading?‘xxx‘)的錯(cuò)誤解決
相關(guān)文章
TypeScript實(shí)現(xiàn)單鏈表的示例代碼
鏈表是一種物理存儲(chǔ)單元上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),本文主要介紹了TypeScript實(shí)現(xiàn)單鏈表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08JavaScript常用代碼書(shū)寫(xiě)規(guī)范的超全面總結(jié)
這篇文章給大家全面總結(jié)了JavaScript常用代碼的書(shū)寫(xiě)規(guī)范,分別利用推薦和不推薦的兩種示例代碼讓大家更能直接的了解書(shū)寫(xiě)規(guī)范,其實(shí)關(guān)于javascript代碼規(guī)范我們應(yīng)該遵循古老的原則:“能做并不意味著應(yīng)該做”,好了,下面我們就來(lái)一起看看吧。2016-09-09layui(1.0.9)文件上傳upload,前后端的實(shí)例代碼
今天小編就為大家分享一篇layui(1.0.9)文件上傳upload,前后端的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09uniapp路由uni-simple-router實(shí)例詳解
uni-simple-router專(zhuān)為uniapp打造的路由器,和uniapp深度集成,這篇文章主要給大家介紹了關(guān)于uniapp路由uni-simple-router的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09兼容FireFox 用javascript寫(xiě)的一個(gè)畫(huà)圖函數(shù)
兼容FireFox 用javascript寫(xiě)的一個(gè)畫(huà)圖函數(shù)...2007-08-08TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié))
這篇文章主要介紹了TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié)),今天我們?cè)賮?lái)實(shí)戰(zhàn)下官方推薦的新的vue狀態(tài)管理工具Pini,感興趣的小伙伴可以參考一下2022-06-06用JavaScript實(shí)現(xiàn)字符串切分功能
用JavaScript實(shí)現(xiàn)字符串切分功能...2007-01-01