I have an app in Vue 3 where I make a call to the back end of my code to get data. The data is returned nested in JSON arrays and objects, and I then assign the data to a property in my component. I am trying to assign the JSON object to data in my parent component and then pass the data as a prop to my child component. However, every time I do this, the data keeps getting assigned as Proxy(Object) which seems to make it more difficult to access the data in the child component.
My code is as follows:
/* Parent Component */
<template>
<div>
<child-component :myData="rawData"></child-component>
</div>
</template>
<script>
import ChildComponent from '../components/ChildComponent.vue';
export default {
components: {
ChildComponent
},
data() {
return {
rawData: {}
}
},
methods: {
getData() {
fetch('/my-endpoint, { method: 'GET' })
.then((response) => response.json())
.then((data) => {
this.rawData = data[0].data[0];
})
}
}
}
</script>
/* Child Component */
<script>
export default {
props: {
myData: {
type: Object
}
},
data() {
return {
displayData: myData
}
}
}
</script>
When I try to console.log() any of the data that holds my fetch call (rawData, myData or displayData) I'm seeing Proxy (Object) in the console, and I can't access any of the data in the object. I see the data in the Vue dev tools plugin, but I cannot use any of it. Even trying to do something like console.log(displayData.id) or console.log(displayData.description), which are properties I know exist in these objects and properties I see in the Vue dev tools plugin, I just get undefined in the console.
What I have tried to do:
import { isProxy, toRaw } from 'vue';in both the parent and child components and thenif(isProxy(myData)) toRaw(myData);(and the same withrawDataanddisplayData). This does not work and the objects are still proxies.JSON.parse(JSON.stringify(myData));in both the parent and child components (and the same withrawDataanddisplayData). This does not work and the objects are still proxies.Out of desperation, changing the prop declaration in the child component to just
props: { myData: Object }and even justprops: ['myData']. This does not work and the objects are still proxies.
How can I access the data in these objects? I'd prefer for them to not be proxy objects, but if I cannot change them then how do I get data if they need to stay as proxies?
getData()function in my parent component to my child component as a prop, the data is inaccessible inconsole.log(). I'd like to use the data in a data property forv-model, which I've also tried and also comes up asundefined. If I put the data in a computed property, then it seems to work but then it is immutable.