切换主题

This commit is contained in:
tangly1024
2023-06-19 22:50:12 +08:00
parent 9759f8fb50
commit 16bdf3a745
3 changed files with 34 additions and 36 deletions

View File

@@ -5,7 +5,7 @@ import BLOG from '@/blog.config'
import { ALL_THEME, initDarkMode } from '@/lib/theme'
import NProgress from 'nprogress'
import LoadingCover from '@/components/LoadingCover'
import { getQueryParam, isBrowser } from './utils'
import { getQueryParam, getQueryVariable, isBrowser } from './utils'
const GlobalContext = createContext()
@@ -46,18 +46,20 @@ export function GlobalContextProvider({ children }) {
NProgress.start()
const { theme } = router.query
if (theme && !url.includes(`theme=${theme}`)) {
// TODO
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${theme}`
console.log('检查newUrl', newUrl)
router.push(newUrl)
}
setOnLoading(true)
}
const handleStop = () => {
NProgress.done()
setTheme(getQueryParam(router.asPath, 'theme') || BLOG.THEME)
setOnLoading(false)
}
const queryTheme = getQueryVariable('theme')
setTheme(queryTheme)
router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeError', handleStop)
router.events.on('routeChangeComplete', handleStop)