question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maybe bug in createGlobalState with using vue-router

See original GitHub issue

store.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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
antfucommented, May 14, 2021

To use router globally, I would recommend to do:

// router.ts

export const router = createRouter()
// main.ts

import { router } from './router'

createApp().use(router).mount()
// whatever

import { router } from '../router'

const route = router.currentRoute

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.2

1reaction
wheatjscommented, May 12, 2021

As a workaround you can declare const router = useRouter() before const state = useState().

I’m thinking the issue comes from withScope here https://github.com/vueuse/vueuse/blob/main/packages/core/createGlobalState/index.ts#L4

Perhaps it has something to do with createApp portion, not sure tbh though.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found