Skip to content

createStore with key get error after vite hot updated #2019

@qiuyaofan

Description

@qiuyaofan

Version

4.0.1

Steps to reproduce

main.js

// main.ts
import { createApp } from 'vue'
import { store, key } from './store'

const app = createApp({ ... })

// 传入 injection key
app.use(store, key)

app.mount('#app')

store.js

import { InjectionKey } from 'vue'
import { createStore, useStore as baseUserStore, Store } from 'vuex';

// 为 store state 声明类型
export interface State {
  count: number
}

// 定义 injection key
export const key: InjectionKey<Store<State>> = Symbol('admin-store')

const store = createStore<State>({
  state: {
    count: 0
  }
})

export default store;

export function useStore<T = State>(): any {
    return baseUserStore<T>(key);
}

index.vue

import { useStore } from './store'

export default {
  setup () {
    const store = useStore() //  undefined

  }
}

What is expected?

when I change index.vue and save, vite finish hot update can correct to get useStore() in setup function

What is actually happening?

when I change index.vue and save, vite finish hot update
useStore() return undefined, and warnning injection "Symbol(admin-store)" not found


but I remove key,it‘s work fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionThis is a question rather than an issue or pull request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions