vue3接口數(shù)據(jù)賦值對象,渲染報錯問題及解決
vue3接口數(shù)據(jù)賦值對象,渲染報錯
const app = require('express')()
// 跨域設(shè)置
app.all("*", function (req, res, next) {
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Origin", '*'); // 添加這一行代碼,代理配置不成功
res.setHeader("Access-Control-Allow-Methods", '*');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since")
next();
})
app.get('/', function (req, res) {
res.send({
dict: 'sfasf',
detailAddress:'1111111'
})
})
app.listen(9999, () => {
console.log('http://localhost:9999')
})用express啟動了一個簡單的node服務(wù)器寫了一個測試接口
vue/cli創(chuàng)建的vue3項目,頁面中通過接口獲取的數(shù)據(jù),注意第二個屬性dict


現(xiàn)在我們用對象賦值的方法將res.data賦值給vue3的reactive里綁定的屬性form
<template>
<div>{{data.form.dict}}</div>
<div>{{data.form.detailAddress}}</div>
</template>
<script setup>
import {reactive} from 'vue';
import axios from 'axios'
const data = reactive({
form:null,
})
axios.get('http://localhost:9999').then(res => {
console.log(res,'res');
if (res.status === 200) {
data.form = res.data
console.log(data.form);
}
})
</script>
<style lang="scss" scoped>
</style>查看打印結(jié)果

報錯了,但是頁面能渲染
如果我們把動態(tài)數(shù)據(jù)綁定的form初始化的值不用null,換成{}
<template>
<div>{{data.form.dict}}</div>
<div>{{data.form.detailAddress}}</div>
</template>
<script setup>
import {reactive} from 'vue';
import axios from 'axios'
const data = reactive({
form:{},
})
axios.get('http://localhost:9999').then(res => {
console.log(res,'res');
if (res.status === 200) {
data.form = res.data
console.log(data.form);
}
})
</script>
<style lang="scss" scoped>
</style>打印結(jié)果

沒有報錯,且能渲染
總結(jié):通過樓主查閱各種文檔的細(xì)心研磨,發(fā)現(xiàn)vue3的動態(tài)數(shù)據(jù)綁定,在進(jìn)行proxy代理的時候,get方法中的return返回時,使用了reflect反射來綁定this的指向
get:function(target,key){
return Reflect.get(target,key)
}我認(rèn)為,如果初始值為null,會導(dǎo)致this指向null,引發(fā)這個報錯問題,目前樓主認(rèn)為是這個原因?qū)е碌摹?/p>
vue在渲染數(shù)據(jù)的時候的一些報錯問題
問題描述
vue在請求獲取數(shù)據(jù)的時候,這個過程是異步的,但是剛進(jìn)頁面的時候,數(shù)據(jù)還未被獲取到,是為空的,如果再次點(diǎn)擊拿下一層數(shù)據(jù)時沒辦法拿到數(shù)據(jù)的,會報錯

解決方法
i 在data中不使用null而是使用{}
ii可以在data中使用null,但是在渲染數(shù)據(jù)的外面的包一個div,使用v-if=" homeData"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- <div v-if="homeData"> -->
<div class="bangItem" v-for="bangItem in homeData.rank_list">
<!-- </div> -->
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
// homeData:null//剛進(jìn)來的時候是null,上面使用rank_list還未獲取數(shù)據(jù),會報錯,解決方法這里不使用Null,用{},方法二這里可以使用null,然后在渲染數(shù)據(jù)外面包一個div,使用v-if homeDate
homeData: {}
},
created() {
this.getHome()
},
methods: {
getHome() {
fetch('http://m.kuwo.cn/newh5app/api/mobile/v1/home').then(res => res.json()).then(res => {
console.log(res);
this.homeData = res.data;
})
}
}
})
</script>
</body>
</html>在vue中請求數(shù)據(jù)的時候,同一個數(shù)據(jù)有的是數(shù)據(jù)是不存在的,有的是存在的,如果直接寫會報錯,這時候可以在沒有的那個數(shù)據(jù)名后面加?l來作為判斷,之后再往下點(diǎn)一層
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vue3中require報錯require is not defined問題及解決
- VUE3刷新頁面報錯問題解決:Uncaught?SyntaxError:Unexpected?token?'<'
- vue3+vite中報錯信息處理方法Error: Module “path“ has been externalized for browser compatibility...
- Vue3報錯‘defineProps‘?is?not?defined的解決方法
- 解決Vue3報錯:Property?“xxx“?was?accessed?during?render?but?is?not?defined?on?instance.
- vue3+vite項目中按需引入vant報錯:Failed?to?resolve?import的解決方案
- vue3.x:報錯清單及解決記錄
相關(guān)文章
vuex + keep-alive實現(xiàn)tab標(biāo)簽頁面緩存功能
這篇文章主要介紹了vuex + keep-alive實現(xiàn)tab標(biāo)簽頁面緩存功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
vue實現(xiàn)導(dǎo)航欄效果(選中狀態(tài)刷新不消失)
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)導(dǎo)航欄效果,選中狀態(tài)刷新不消失,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
從零搭建一個vite+vue3+ts規(guī)范基礎(chǔ)項目(搭建過程問題小結(jié))
這篇文章主要介紹了從零搭建一個vite+vue3+ts規(guī)范基礎(chǔ)項目,本項目已vite開始,所以按照vite官方的命令開始,對vite+vue3+ts項目搭建過程感興趣的朋友一起看看吧2022-05-05
vue使用html2canvas和jspdf將html轉(zhuǎn)成pdf
在前端開發(fā)中, html轉(zhuǎn)pdf是最常見的需求,下面這篇文章主要給大家介紹了關(guān)于vue如何使用html2canvas和jspdf將html轉(zhuǎn)成pdf的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實現(xiàn)
這篇文章主要介紹了搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

