typeError:无法读取未定义的属性(读取(读取' __ ob __')

发布于 2025-01-31 15:53:48 字数 2401 浏览 0 评论 0原文

从一个带有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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在你怀里撒娇 2025-02-07 15:53:48

我也有同样的问题,我解决了有关组件的静止道具:

<Nuxt keep-alive/>

官方doc

https://nuxtjs.org/docs/features/nuxt-components/

[update]

您还可以检查您是否有一些组件:

   data() {
     return {

     };
   },

似乎这是主要问题

I have the same issue, i resolved with keep-alive props on component:

<Nuxt keep-alive/>

More info on official doc

https://nuxtjs.org/docs/features/nuxt-components/

[Update]

You can also check if you have on some component:

   data() {
     return {

     };
   },

Seem it is the main problem

醉南桥 2025-02-07 15:53:48

在我的情况下,我没有从中返回对象

data() {}

应该是

data() { return {} }

至少 正确的。请检查与“ Data(){}应返回对象”有关的控制台错误

in my case issue was that i was not returning an object from

data() {}

it should be

data() { return {} }

at least to be correct. Please check console error related to "data(){} should return an object"

梦幻的心爱 2025-02-07 15:53:48

该问题来自组件悬停事件,与上面的代码段无关。

The issue was coming from a component hover event, nothing related to the code snippet above.

薄荷港 2025-02-07 15:53:48

也许您可以将创建的异步功能使用。或者最好仍然使用NUXT提供的获取。

 asynce fetch() {
    this.productsLoading = true
    try {
      const data = await this.$axios.$get('api/products')
      this.products = data
      this.productsLoading = false
      
    } catch (err) {
      this.productsLoading = false
      return err
    }
 },

我也认为您不需要回报

Maybe you can just use the created with the async functiion. or better still use the fetch provided by Nuxt.

 asynce fetch() {
    this.productsLoading = true
    try {
      const data = await this.$axios.$get('api/products')
      this.products = data
      this.productsLoading = false
      
    } catch (err) {
      this.productsLoading = false
      return err
    }
 },

Also i dont think you will need a return in the promise

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文