Skip to content
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

After build, it occur: Cannot read property 'state' of undefined #156

Closed
leon-kfd opened this issue May 15, 2020 · 3 comments
Closed

After build, it occur: Cannot read property 'state' of undefined #156

leon-kfd opened this issue May 15, 2020 · 3 comments

Comments

@leon-kfd
Copy link

leon-kfd commented May 15, 2020

My apps has used vuex, and it can run in the development environment.
But when i run vite build, it happen this error on browser:

index.js:9 Uncaught TypeError: Cannot read property 'state' of undefined
    at index.js:9
    at n (index.js:1)
    at Object.get value [as value] (index.js:1)
    at Object.get (index.js:1)
    at Object.get (index.js:1)
    at index.js:9
    at Vn (index.js:1)
    at Proxy.<anonymous> (index.js:9)
    at Proxy.<anonymous> (index.js:1)
    at nt (index.js:1)
(anonymous) @ index.js:9
n @ index.js:1
get value @ index.js:1
(anonymous) @ index.js:1
get @ index.js:1
(anonymous) @ index.js:9
Vn @ index.js:1
(anonymous) @ index.js:9
(anonymous) @ index.js:1
nt @ index.js:1
(anonymous) @ index.js:1
n @ index.js:1
G @ index.js:1
$ @ index.js:1
V @ index.js:1
j @ index.js:1
S @ index.js:1
P @ index.js:1
T @ index.js:1
C @ index.js:1
S @ index.js:1
P @ index.js:1
T @ index.js:1
C @ index.js:1
S @ index.js:1
P @ index.js:1
T @ index.js:1
C @ index.js:1
Show 28 more frames

This seems to be because vuex can't pack?
This is my code which use vuex $store.

<script lang="ts">
import { ref, computed, getCurrentInstance } from 'vue'
import Card from './Card.vue'
export default {
  name: 'Main',
  components: {
    Card
  },
  setup(props) {
    const instance: any = getCurrentInstance()
    const ctx = instance.ctx
    const selectedDate = computed(() => ctx.$store.state.selectedDate)
    return {
      selectedDate
    }
  }
}
</script>

And I don't know if I wrote it wrong.

@ais-one
Copy link
Contributor

ais-one commented May 15, 2020

Hi,

For working vuex and vue-router...

You can refer to example below. It is using next version of vuex for use with vue 3

https://github.com/ais-one/vue-crud-x/tree/develop/example-app/web/vite

Take note of versioning (you can refer to package.json). I am using vite 0.14.4 for now

@yyx990803
Copy link
Member

yyx990803 commented May 15, 2020

Why are people already using the internal instance and the ctx? This is so wrong. If you are using the composition API, use provide/inject. Avoid getCurrentInstance() in any way possible. It's only meant for libraries.

Relying on instance in setup() is code smell.

@yyx990803
Copy link
Member

Closing because vuex@next is verified to be working with current release of Vite. Please make sure you are using the correct version of vuex and following the correct usage: https://github.com/vuejs/vuex/tree/4.0

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants