typeError:无法读取未定义的属性(读取(读取' __ ob __')
从一个带有ProductCard组件到另一个页面的页面导航时出现错误。 我相信错误来自数据获取或已安装()
,但我无法解决。 ProductCard组件只是带有一些道具的视觉组件。因此错误必须在这里。
完整错误:
client.js:228 TypeError: Cannot read properties of undefined (reading '__ob__')
at VueComponent.Vue.$destroy (vue.runtime.esm.js:4004:18)
at destroy (vue.runtime.esm.js:3175:27)
at invokeDestroyHook (vue.runtime.esm.js:6148:59)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at VueComponent.patch [as __patch__] (vue.runtime.esm.js:6501:30)
at VueComponent.Vue.$destroy (vue.runtime.esm.js:4010:8)
at destroy (vue.runtime.esm.js:3175:27)
at invokeDestroyHook (vue.runtime.esm.js:6148:59)
我的页面.vue
文件模板:
<template>
<main>
<ProductTabs></ProductTabs>
<div
v-if="productsLoading"
class="spinner-border"
style="width: 3rem; height: 3rem"
role="status"
>
<span class="sr-only">Loading...</span>
</div>
<v-container v-else fluid>
<v-row d-flex justify="center">
<ProductCard
v-for="product in products"
:key="product._id"
:product-title="product.productName"
:product-price="product.price"
:product-img1="product.img1"
:product-img2="product.img2"
></ProductCard>
<br />
</v-row>
</v-container>
</main>
</template>
我的页面.vue
文件脚本:
<script>
export default {
path: '/',
name: 'ProductsPage',
components: { ProductTabs },
// variables
data() {
return {
products: [],
productsLoading: false,
}
},
// call the get Poducts method
mounted() {
this.getAllProducts()
},
// get products from api and save into products array
methods: {
async getAllProducts() {
this.productsLoading = true
try {
const data = await this.$axios.$get('api/products')
this.products = data
this.productsLoading = false
return this.products
} catch (err) {
this.productsLoading = false
return err
}
},
},
}
</script>
The error appears when navigating from one page with the ProductCard component to another.
I believe the error comes from the data fetching or the mounted()
, but I haven't been able to solve it. The ProductCard component is just a visual one with some props. So the error must be here.
Full error:
client.js:228 TypeError: Cannot read properties of undefined (reading '__ob__')
at VueComponent.Vue.$destroy (vue.runtime.esm.js:4004:18)
at destroy (vue.runtime.esm.js:3175:27)
at invokeDestroyHook (vue.runtime.esm.js:6148:59)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at invokeDestroyHook (vue.runtime.esm.js:6153:9)
at VueComponent.patch [as __patch__] (vue.runtime.esm.js:6501:30)
at VueComponent.Vue.$destroy (vue.runtime.esm.js:4010:8)
at destroy (vue.runtime.esm.js:3175:27)
at invokeDestroyHook (vue.runtime.esm.js:6148:59)
My page .vue
file template:
<template>
<main>
<ProductTabs></ProductTabs>
<div
v-if="productsLoading"
class="spinner-border"
style="width: 3rem; height: 3rem"
role="status"
>
<span class="sr-only">Loading...</span>
</div>
<v-container v-else fluid>
<v-row d-flex justify="center">
<ProductCard
v-for="product in products"
:key="product._id"
:product-title="product.productName"
:product-price="product.price"
:product-img1="product.img1"
:product-img2="product.img2"
></ProductCard>
<br />
</v-row>
</v-container>
</main>
</template>
My page .vue
file script:
<script>
export default {
path: '/',
name: 'ProductsPage',
components: { ProductTabs },
// variables
data() {
return {
products: [],
productsLoading: false,
}
},
// call the get Poducts method
mounted() {
this.getAllProducts()
},
// get products from api and save into products array
methods: {
async getAllProducts() {
this.productsLoading = true
try {
const data = await this.$axios.$get('api/products')
this.products = data
this.productsLoading = false
return this.products
} catch (err) {
this.productsLoading = false
return err
}
},
},
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也有同样的问题,我解决了有关组件的静止道具:
官方doc
https://nuxtjs.org/docs/features/nuxt-components/
[update]
您还可以检查您是否有一些组件:
似乎这是主要问题
I have the same issue, i resolved with keep-alive props on component:
More info on official doc
https://nuxtjs.org/docs/features/nuxt-components/
[Update]
You can also check if you have on some component:
Seem it is the main problem
在我的情况下,我没有从中返回对象
应该是
至少 正确的。请检查与“ Data(){}应返回对象”有关的控制台错误
in my case issue was that i was not returning an object from
it should be
at least to be correct. Please check console error related to "data(){} should return an object"
该问题来自组件悬停事件,与上面的代码段无关。
The issue was coming from a component hover event, nothing related to the code snippet above.
也许您可以将创建的异步功能使用。或者最好仍然使用NUXT提供的获取。
我也认为您不需要回报
Maybe you can just use the created with the async functiion. or better still use the fetch provided by Nuxt.
Also i dont think you will need a return in the promise