refactor: migrate from Vuex to Pinia
This commit is contained in:
+2
-2
@@ -27,10 +27,10 @@
|
|||||||
"@coreui/vue-chartjs": "^3.0.0",
|
"@coreui/vue-chartjs": "^3.0.0",
|
||||||
"@popperjs/core": "^2.11.8",
|
"@popperjs/core": "^2.11.8",
|
||||||
"chart.js": "^4.4.2",
|
"chart.js": "^4.4.2",
|
||||||
|
"pinia": "^2.1.7",
|
||||||
"simplebar-vue": "^2.3.3",
|
"simplebar-vue": "^2.3.3",
|
||||||
"vue": "^3.4.21",
|
"vue": "^3.4.21",
|
||||||
"vue-router": "^4.3.0",
|
"vue-router": "^4.3.0"
|
||||||
"vuex": "^4.1.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@vitejs/plugin-vue": "^5.0.4",
|
"@vitejs/plugin-vue": "^5.0.4",
|
||||||
|
|||||||
+7
-4
@@ -1,10 +1,13 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { onBeforeMount } from 'vue'
|
import { onBeforeMount } from 'vue'
|
||||||
import { useStore } from 'vuex'
|
|
||||||
import { useColorModes } from '@coreui/vue'
|
import { useColorModes } from '@coreui/vue'
|
||||||
|
|
||||||
const { isColorModeSet, setColorMode } = useColorModes('coreui-free-vue-admin-template-theme')
|
import { useThemeStore } from '@/stores/theme.js'
|
||||||
const store = useStore()
|
|
||||||
|
const { isColorModeSet, setColorMode } = useColorModes(
|
||||||
|
'coreui-free-vue-admin-template-theme',
|
||||||
|
)
|
||||||
|
const currentTheme = useThemeStore()
|
||||||
|
|
||||||
onBeforeMount(() => {
|
onBeforeMount(() => {
|
||||||
const urlParams = new URLSearchParams(window.location.href.split('?')[1])
|
const urlParams = new URLSearchParams(window.location.href.split('?')[1])
|
||||||
@@ -23,7 +26,7 @@ onBeforeMount(() => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
setColorMode(store.state.theme)
|
setColorMode(currentTheme.theme)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import { useColorModes } from '@coreui/vue'
|
import { useColorModes } from '@coreui/vue'
|
||||||
import AppBreadcrumb from './AppBreadcrumb'
|
|
||||||
import AppHeaderDropdownAccnt from './AppHeaderDropdownAccnt'
|
import AppBreadcrumb from '@/components/AppBreadcrumb.vue'
|
||||||
|
import AppHeaderDropdownAccnt from '@/components/AppHeaderDropdownAccnt.vue'
|
||||||
|
import { useSidebarStore } from '@/stores/sidebar.js'
|
||||||
|
|
||||||
const headerClassNames = ref('mb-4 p-0')
|
const headerClassNames = ref('mb-4 p-0')
|
||||||
const { colorMode, setColorMode } = useColorModes('coreui-free-vue-admin-template-theme')
|
const { colorMode, setColorMode } = useColorModes('coreui-free-vue-admin-template-theme')
|
||||||
|
const sidebar = useSidebarStore()
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
document.addEventListener('scroll', () => {
|
document.addEventListener('scroll', () => {
|
||||||
@@ -21,7 +24,7 @@ onMounted(() => {
|
|||||||
<template>
|
<template>
|
||||||
<CHeader position="sticky" :class="headerClassNames">
|
<CHeader position="sticky" :class="headerClassNames">
|
||||||
<CContainer class="border-bottom px-4" fluid>
|
<CContainer class="border-bottom px-4" fluid>
|
||||||
<CHeaderToggler @click="$store.commit('toggleSidebar')" style="margin-inline-start: -14px">
|
<CHeaderToggler @click="sidebar.toggleVisible()" style="margin-inline-start: -14px">
|
||||||
<CIcon icon="cil-menu" size="lg" />
|
<CIcon icon="cil-menu" size="lg" />
|
||||||
</CHeaderToggler>
|
</CHeaderToggler>
|
||||||
<CHeaderNav class="d-none d-md-flex">
|
<CHeaderNav class="d-none d-md-flex">
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed } from 'vue'
|
|
||||||
import { RouterLink } from 'vue-router'
|
import { RouterLink } from 'vue-router'
|
||||||
import { useStore } from 'vuex'
|
|
||||||
import { AppSidebarNav } from './AppSidebarNav'
|
|
||||||
import { logo } from '@/assets/brand/logo'
|
import { logo } from '@/assets/brand/logo'
|
||||||
import { sygnet } from '@/assets/brand/sygnet'
|
import { sygnet } from '@/assets/brand/sygnet'
|
||||||
|
import { AppSidebarNav } from '@/components/AppSidebarNav.js'
|
||||||
|
import { useSidebarStore } from '@/stores/sidebar.js'
|
||||||
|
|
||||||
const store = useStore()
|
const sidebar = useSidebarStore()
|
||||||
const sidebarUnfoldable = computed(() => store.state.sidebarUnfoldable)
|
|
||||||
const sidebarVisible = computed(() => store.state.sidebarVisible)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -17,15 +14,9 @@ const sidebarVisible = computed(() => store.state.sidebarVisible)
|
|||||||
class="border-end"
|
class="border-end"
|
||||||
colorScheme="dark"
|
colorScheme="dark"
|
||||||
position="fixed"
|
position="fixed"
|
||||||
:unfoldable="sidebarUnfoldable"
|
:unfoldable="sidebar.unfoldable"
|
||||||
:visible="sidebarVisible"
|
:visible="sidebar.visible"
|
||||||
@visible-change="
|
@visible-change="(value) => sidebar.toggleVisible(value)"
|
||||||
(event) =>
|
|
||||||
$store.commit({
|
|
||||||
type: 'updateSidebarVisible',
|
|
||||||
value: event,
|
|
||||||
})
|
|
||||||
"
|
|
||||||
>
|
>
|
||||||
<CSidebarHeader class="border-bottom">
|
<CSidebarHeader class="border-bottom">
|
||||||
<RouterLink custom to="/" v-slot="{ href, navigate }">
|
<RouterLink custom to="/" v-slot="{ href, navigate }">
|
||||||
@@ -34,11 +25,11 @@ const sidebarVisible = computed(() => store.state.sidebarVisible)
|
|||||||
<CIcon custom-class-name="sidebar-brand-narrow" :icon="sygnet" :height="32" />
|
<CIcon custom-class-name="sidebar-brand-narrow" :icon="sygnet" :height="32" />
|
||||||
</CSidebarBrand>
|
</CSidebarBrand>
|
||||||
</RouterLink>
|
</RouterLink>
|
||||||
<CCloseButton class="d-lg-none" dark @click="$store.commit('toggleSidebar')" />
|
<CCloseButton class="d-lg-none" dark @click="sidebar.toggleVisible()" />
|
||||||
</CSidebarHeader>
|
</CSidebarHeader>
|
||||||
<AppSidebarNav />
|
<AppSidebarNav />
|
||||||
<CSidebarFooter class="border-top d-none d-lg-flex">
|
<CSidebarFooter class="border-top d-none d-lg-flex">
|
||||||
<CSidebarToggler @click="$store.commit('toggleUnfoldable')" />
|
<CSidebarToggler @click="sidebar.toggleUnfoldable()" />
|
||||||
</CSidebarFooter>
|
</CSidebarFooter>
|
||||||
</CSidebar>
|
</CSidebar>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
+3
-2
@@ -1,7 +1,8 @@
|
|||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
|
import { createPinia } from 'pinia'
|
||||||
|
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from './router'
|
import router from './router'
|
||||||
import store from './store'
|
|
||||||
|
|
||||||
import CoreuiVue from '@coreui/vue'
|
import CoreuiVue from '@coreui/vue'
|
||||||
import CIcon from '@coreui/icons-vue'
|
import CIcon from '@coreui/icons-vue'
|
||||||
@@ -9,7 +10,7 @@ import { iconsSet as icons } from '@/assets/icons'
|
|||||||
import DocsExample from '@/components/DocsExample'
|
import DocsExample from '@/components/DocsExample'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
app.use(store)
|
app.use(createPinia())
|
||||||
app.use(router)
|
app.use(router)
|
||||||
app.use(CoreuiVue)
|
app.use(CoreuiVue)
|
||||||
app.provide('icons', icons)
|
app.provide('icons', icons)
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
import { createStore } from 'vuex'
|
|
||||||
|
|
||||||
export default createStore({
|
|
||||||
state: {
|
|
||||||
sidebarVisible: '',
|
|
||||||
sidebarUnfoldable: false,
|
|
||||||
theme: 'light',
|
|
||||||
},
|
|
||||||
mutations: {
|
|
||||||
toggleSidebar(state) {
|
|
||||||
state.sidebarVisible = !state.sidebarVisible
|
|
||||||
},
|
|
||||||
toggleUnfoldable(state) {
|
|
||||||
state.sidebarUnfoldable = !state.sidebarUnfoldable
|
|
||||||
},
|
|
||||||
updateSidebarVisible(state, payload) {
|
|
||||||
state.sidebarVisible = payload.value
|
|
||||||
},
|
|
||||||
},
|
|
||||||
actions: {},
|
|
||||||
modules: {},
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
import { defineStore } from 'pinia'
|
||||||
|
|
||||||
|
export const useSidebarStore = defineStore('sidebar', () => {
|
||||||
|
const visible = ref(undefined)
|
||||||
|
const unfoldable = ref(false)
|
||||||
|
|
||||||
|
const toggleVisible = (value) => {
|
||||||
|
visible.value = value !== undefined ? value : !visible.value
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleUnfoldable = () => {
|
||||||
|
unfoldable.value = !unfoldable.value
|
||||||
|
}
|
||||||
|
|
||||||
|
return { visible, unfoldable, toggleVisible, toggleUnfoldable }
|
||||||
|
})
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
import { defineStore } from 'pinia'
|
||||||
|
|
||||||
|
export const useThemeStore = defineStore('theme', () => {
|
||||||
|
const theme = ref('light')
|
||||||
|
|
||||||
|
const toggleTheme = (_theme) => {
|
||||||
|
theme.value = _theme
|
||||||
|
}
|
||||||
|
|
||||||
|
return { theme, toggleTheme }
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user