nuxt-community / nuxt-property-decorator

Property decorators for Nuxt (base on vue-property-decorator)
https://github.com/kaorun343/vue-property-decorator
MIT License
399 stars 34 forks source link

Cannot read property of undefined #85

Open Pacheco95 opened 3 years ago

Pacheco95 commented 3 years ago

I'm trying to access data in my store inside my component, but the getter is being called before the created() method lifecycle hook and this is causing the problem TypeError: Cannot read property 'loading' of undefined

<template>
  <div class="container">{{ loading }}</div>
</template>

<script lang="ts">
import { Component, getModule, Vue } from "nuxt-property-decorator";
import ExampleStore from "~/store/example-store";

@Component
export default class Hello extends Vue {
  exampleStore = getModule(ExampleStore, this.$store);

  get loading() {
    console.log("getter");
    return this.exampleStore.loading;
  }

  created() {
    console.log("created?");
  }
}
</script>

<style>
.container {
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
</style>

Reproductible code https://codesandbox.io/s/exciting-easley-e98j0?file=/pages/index.vue