Skip to content

this.$store is undefined in a custom components inside a <v-template> #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
msaelices opened this issue Apr 7, 2019 · 3 comments
Closed
Labels
priority:normal V2 Issues related to V2

Comments

@msaelices
Copy link

msaelices commented Apr 7, 2019

Version

2.2.1

Reproduction link

https://github.com/msaelices/vuex-error-in-v-template

Platform and OS info

NS 5.3

Steps to reproduce

Let review the following Vue application with Vuex (see the github repo for a complete app):

app/components/App.vue

<template>
    <Page>
        <ActionBar title="App for checking NS-vue #473 issue"/>
        <GridLayout columns="*" rows="*">
            <ListView for="item in items">
              <v-template>
                <MyComponent :item="item" />
              </v-template>
            </ListView>
        </GridLayout>
    </Page>
</template>
<script >
  import MyComponent from './MyComponent'

  export default {
    components: {
      MyComponent,
    },
    computed: {
      items () {
        return this.$store.state.items
      }
    }
  }
</script>

app/components/MyComponent.vue

<template>
  <Label :text="item.text" col="0" row="0" @tap="changeText" />
</template>
<script >
  export default {
    props: ['item'],
    methods: {
      changeText () {
        this.$store.dispatch('changeText', {
          item: this.item,
          text: 'Other Text',
        })
      },
    },
  }
</script>

The issue happens when the user clicks on some of the items in the list.

What is expected?

The action should be dispatched

What is actually happening?

this.$store undefined error is got

The root of the problem seems to be placing a custom component inside a <v-template> block, as I tested using the same custom component outside the <v-template> and it worked.

@msaelices
Copy link
Author

@rigor789 I'm wondering if this issue may be related to #229 (comment)

@rigor789
Copy link
Member

rigor789 commented Apr 7, 2019

Could be related, but the issue is the odd parent/child relations in some components (ListView/v-template, Frame etc). The workaround for now is to register the store to the vue prototype as well: Vue.prototype.$store = store - this way you can access the store from any Vue component, and not just descendants of the root vue instance.

Also the comment you linked is no longer the case, I've since refactored it and it doesn't need to create proxies anymore.

@elie-g
Copy link
Contributor

elie-g commented May 24, 2019

Seems to be related to nativescript-vue/nativescript-vue-navigator#5

They are both related to vuex and they have the same workaround.
They might be the same issue (odd parent/child relations).

@rigor789 rigor789 added the V2 Issues related to V2 label Dec 5, 2022
@rigor789 rigor789 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:normal V2 Issues related to V2
Development

No branches or pull requests

4 participants