mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
feat(bridge): init function for useState
(#773)
This commit is contained in:
parent
3c132a8de0
commit
2fb58ae2be
@ -1,4 +1,4 @@
|
|||||||
import { reactive, toRef, isReactive } from '@vue/composition-api'
|
import { reactive, toRef, isReactive, Ref } from '@vue/composition-api'
|
||||||
import type VueRouter from 'vue-router'
|
import type VueRouter from 'vue-router'
|
||||||
import type { Route } from 'vue-router'
|
import type { Route } from 'vue-router'
|
||||||
import { useNuxtApp } from './app'
|
import { useNuxtApp } from './app'
|
||||||
@ -41,7 +41,7 @@ export const useRoute = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// payload.state is used for vuex by nuxt 2
|
// payload.state is used for vuex by nuxt 2
|
||||||
export const useState = (key: string) => {
|
export const useState = <T>(key: string, init?: (() => T)): Ref<T> => {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
if (!nuxtApp.payload.useState) {
|
if (!nuxtApp.payload.useState) {
|
||||||
nuxtApp.payload.useState = {}
|
nuxtApp.payload.useState = {}
|
||||||
@ -49,5 +49,9 @@ export const useState = (key: string) => {
|
|||||||
if (!isReactive(nuxtApp.payload.useState)) {
|
if (!isReactive(nuxtApp.payload.useState)) {
|
||||||
nuxtApp.payload.useState = reactive(nuxtApp.payload.useState)
|
nuxtApp.payload.useState = reactive(nuxtApp.payload.useState)
|
||||||
}
|
}
|
||||||
return toRef(nuxtApp.payload.useState, key)
|
const state = toRef(nuxtApp.payload.useState, key)
|
||||||
|
if (state.value === undefined && init) {
|
||||||
|
state.value = init()
|
||||||
|
}
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user