当前位置:网站首页>问题:访问组件中数据object(定义的数据)中属性也为object对象中的属性时,报错现象
问题:访问组件中数据object(定义的数据)中属性也为object对象中的属性时,报错现象
2022-06-23 05:17:00 【小XiaXia】
父组件:
export default {
setup(){
const route=useRoute()
let state=reactive({
list:[],
playList:{
}
})
provide("playList",computed(()=>state.playList))
onMounted(async ()=>{
console.log(route,'route')
let id=route.query.id
let res=await getPlayListDetail(id)
console.log(res,'歌单详情')
state.playList=res.data.playlist
})
return{
state
}
},
components:{
ListViewTop
}
}
子组件:
界面
<div class="contentRight">
<h3>{
{playList.name}}</h3>
<div class="author">
<img :src="playList.creator.avatarUrl" class="header">
<span>{
{playList.creator.nickname}}</span>
</div>
<div class="description">
{
{playList.description}}
</div>
</div>
</div>
js代码
setup(){
//省略
let playList=inject('playList')
}
说明:
子组件中使用playList.creator.avatarUrl会报错,提示不存在,是因为初始playList为空,直接访问里面数组或者对象的属性,如creator的属性会报错
更正后父组件:
export default {
setup(){
const route=useRoute()
let state=reactive({
list:[],
playList:{
//因为初始playList为空,直接访问里面数组或者对象的属性,如creator的属性会报错
//可以手动测试
// let person={
// age;12
// }
// console.log(person.age)//undefined
// console.log(person.friend.name)//报错
creator:{
},//初始化
tracks:[]
}
})
provide("playList",computed(()=>state.playList))
onMounted(async ()=>{
console.log(route,'route')
let id=route.query.id
let res=await getPlayListDetail(id)
console.log(res,'歌单详情')
state.playList=res.data.playlist
})
return{
state
}
},
components:{
ListViewTop
}
}
边栏推荐
- 从 WAN 到 SD-WAN 边缘设备的网络架构
- Given a node of a binary tree, return the successor node of the node
- 什么是PDCA循环?如何整合 PDCA 循环和 OKR
- C语言去除字符串尾部的换行(或其他字符)
- Possible pits in mongodb project
- Index - MySQL
- SAP execution transaction code mrrl error -no message was found for partner 100065-
- Paper notes: multi label learning lsml
- How to maintain secure encryption of email communication with FDA?
- I heard you want to embed ppt on WordPress website?
猜你喜欢

Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?

什么是客户体验自动化?

Day_ 04 smart health project - appointment management - package management

原址 交换

Day_02 传智健康项目-预约管理-检查项管理

How to maintain secure encryption of email communication with FDA?

索引——MySQL

haas506 2.0开发教程-hota(仅支持2.2以上版本)

js创建数组(元素都是对象)

【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%
随机推荐
Plot+seaborn+folium: a visual exploration of Abbey's rental housing data
Day_ 12 smart health project jasperreports
熟练利用切片操作
Coordinate transformation
如何实现与FDA保持邮件通信安全加密?
记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理
Day_04 传智健康项目-预约管理-套餐管理
云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台
百度URL參數之LINK?URL參數加密解密研究(代碼實例)
业务逻辑安全思路总结
C语言 踩坑:文档编码错误,导致base64中文编码错误
Day_ 08 smart health project - mobile terminal development - physical examination appointment
Global attribute lang attribute
How to maintain secure encryption of email communication with FDA?
Index - MySQL
图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
图解 Google V8 # 18 :异步编程(一):V8是如何实现微任务的?
Day_ 06 smart health project - mobile terminal development - physical examination appointment
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
Day_01 传智健康项目-项目概述和环境搭建