Maybe bug in createGlobalState with using vue-router
See original GitHub issuestore.ts
import {createGlobalState, useStorage} from '@vueuse/core'
export const useState = createGlobalState(() => {
return useStorage('user', {
user: null
})
})
main.ts
createApp(App)
.use(router)
.mount('#app')
app.vue
import {useRouter} from 'vue-router'
import {useState} from '~/store'
const state = useState()
const router = useRouter()
console.log(router)
console.log(state.value)
The browser throws the warn “[Vue warn]: inject() can only be used inside setup() or functional components. at <App>” and router undefined in console.
But if in main.ts added line .provide('state', useState())
then warn gone…
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Maybe bug in createGlobalState with using vue-router
As a workaround you can declare const router = useRouter() before const state = useState() . I'm thinking the issue comes from withScope...
Read more >`useRoute` and `useRouter` return undefined if called after an ...
I'm using createGlobalState from @vueuse/core - seems like anything that would delay the setup function would prompt that error?
Read more >vueuse - Bountysource
I'd like to access to the json body response where I can access to the details of the error (statusText is irrelevant in...
Read more >cannot find module '@vue/composition-api' or its ... - You.com
I have a fresh project which I created using the vue command line tool. vue create . But when I type npm serve,...
Read more >element-plus - UNPKG
packages/hooks/use-global-config/index.ts",". ... fn(param));\n };\n return {\n on,\n off,\n trigger\n };\n}\n\nfunction createGlobalState(stateFactory) {\n ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To use router globally, I would recommend to do:
createGlobalState
is only provided as a workaround for now. To have full support of it, we will need https://github.com/vuejs/rfcs/pull/212, which likely be Vue 3.2As a workaround you can declare
const router = useRouter()
beforeconst state = useState()
.I’m thinking the issue comes from
withScope
here https://github.com/vueuse/vueuse/blob/main/packages/core/createGlobalState/index.ts#L4Perhaps it has something to do with
createApp
portion, not sure tbh though.