国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

javascript - vue 初始化數(shù)據(jù)賦值報錯

瀏覽:114日期:2023-05-24 11:10:36

問題描述

vue代碼

<script>import axios from ’axios’;export default { data() {return { titleList: [],} }, created() {this.axios.get(’XX’).then(function(response) { console.log(response.data); this.titleList=response.data;}).catch(function (error) { console.log(error);}); }}</script>報錯

TypeError: Cannot set property ’titleList’ of undefined類型錯誤,不能設置未定義的屬性,

數(shù)據(jù)

response.data是一個對象數(shù)組我已經(jīng)初始化了titleList,不知為何說他未定義,求大神解答

問題解答

回答1:

this 指向更改了 你可以打印出this來看一下指向誰

解決方案

1.用箭頭函數(shù)吧 2.保存this (let _this = this)

回答2:

.then(res => { this.titleList = res;})回答3:

this.axios.get(’XX’) .then(function (response) { response=response.body; this.titleList=response.data; }) .catch(function (error) { console.log(error);})

這樣試下。如果不行,把錯誤貼出看下!

回答4:

this指針丟失,可以使用箭頭函數(shù),也可以用一個變量保存this let _this = this

回答5:

我在使用axios請求數(shù)據(jù)的時候記得是在程序入口文件main.js里面全局引入axios類庫,試試引入后用Vue.prototype.$http=axios,之后就可以在全局使用了,至于樓上給出的答案指出的this指針問題,可以試試,我習慣了es6的語法,所以項目中用的一般都是箭頭函數(shù)

回答6:

axios.get(’***’).then((res) => { this.titleList=res.data;});

使用這種方式試試

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 石阡县| 丰县| 忻州市| 南木林县| 航空| 视频| 海宁市| 宣威市| 闽侯县| 邵阳市| 启东市| 罗定市| 天门市| 丽水市| 白城市| 高阳县| 佛冈县| 抚顺市| 卓尼县| 卢湾区| 广汉市| 禄丰县| 绥江县| 连云港市| 洛南县| 黄骅市| 昌都县| 新干县| 三门县| 涿鹿县| 平安县| 小金县| 宁陵县| 房产| 德江县| 沁源县| 宁乡县| 巴彦县| 五莲县| 习水县| 区。|