mirror of
https://github.com/talktao/Vue3-Vite-Vant-TS-H5.git
synced 2025-04-06 03:57:55 +08:00
feat:pinia初探
This commit is contained in:
parent
e52d8af1cf
commit
1b5844759b
@ -1,19 +1,18 @@
|
|||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from './router'
|
import router from './router'
|
||||||
import store from './store'
|
import {vuexStore, piniaStore} from './store'
|
||||||
|
|
||||||
// 移动端适配
|
// 移动端适配
|
||||||
import 'lib-flexible/flexible.js';
|
import 'lib-flexible/flexible.js';
|
||||||
|
|
||||||
// 引入全局样式
|
// 引入全局样式
|
||||||
import '@/assets/scss/index.scss';
|
import '@/assets/scss/index.scss';
|
||||||
|
|
||||||
// 全局引入按需引入UI库 vant
|
// 全局引入按需引入UI库 vant
|
||||||
import { vantPlugins } from './plugins/vant.js'
|
import { vantPlugins } from './plugins/vant.js'
|
||||||
|
|
||||||
//全局组件
|
//全局公共组件
|
||||||
import components from './plugins/components.js';
|
import components from './plugins/components.js';
|
||||||
|
|
||||||
// import CustomHeader from 'components/CustomHeader/CustomHeader'
|
createApp(App).use(vuexStore).use(piniaStore).use(router).use(vantPlugins).use(components).mount('#app')
|
||||||
|
|
||||||
createApp(App).use(store).use(router).use(vantPlugins).use(components).mount('#app')
|
|
@ -1,9 +1,10 @@
|
|||||||
import { createStore } from "vuex";
|
import { createStore } from "vuex";
|
||||||
|
import { createPinia } from "pinia"
|
||||||
|
|
||||||
const store = createStore({
|
export const vuexStore = createStore({
|
||||||
state: {
|
state: {
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
userNmae: ""
|
userName: ""
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ const store = createStore({
|
|||||||
},
|
},
|
||||||
|
|
||||||
getUserNmae(state,data) {
|
getUserNmae(state,data) {
|
||||||
state.userNmae = data
|
state.userName = data
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
@ -23,4 +24,6 @@ const store = createStore({
|
|||||||
},
|
},
|
||||||
modules: {},
|
modules: {},
|
||||||
});
|
});
|
||||||
export default store;
|
|
||||||
|
|
||||||
|
export const piniaStore = createPinia()
|
||||||
|
27
src/store/testPinia.ts
Normal file
27
src/store/testPinia.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
import { defineStore } from "pinia"
|
||||||
|
export const usePiniaState = defineStore('pinia', ()=>{
|
||||||
|
const userName = ref('')
|
||||||
|
const getUserNmae = (data) => {
|
||||||
|
userName.value = data
|
||||||
|
}
|
||||||
|
return { userName, getUserNmae}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// export const usePiniaState = defineStore({
|
||||||
|
// id: 'textPinia',
|
||||||
|
// state: () => {
|
||||||
|
// return {
|
||||||
|
// userName: ''
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// getters: {
|
||||||
|
|
||||||
|
// },
|
||||||
|
// actions: {
|
||||||
|
// getUserNmae(data) {
|
||||||
|
// this.userName = data
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// })
|
@ -1,15 +1,31 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { useStore } from 'vuex'
|
import { useStore } from 'vuex'
|
||||||
|
import { storeToRefs } from 'pinia'
|
||||||
|
import { usePiniaState } from '@/store/testPinia'
|
||||||
|
|
||||||
import logo from '@/assets/logo.png'
|
import logo from '@/assets/logo.png'
|
||||||
|
|
||||||
const store = useStore()
|
// vuex
|
||||||
|
// const vuexStore = useStore()
|
||||||
|
|
||||||
const userName = computed(() => store.state.userNmae)
|
// pinia
|
||||||
|
const piniaStore = usePiniaState()
|
||||||
|
|
||||||
|
// vuex
|
||||||
|
// const userName = computed(() => vuexStore.state.userNmae)
|
||||||
|
|
||||||
|
// 通过storeToRefs方法将存储在pinia里的数据解构出来,保持state响应性
|
||||||
|
const { userName } = storeToRefs(piniaStore)
|
||||||
|
const { getUserNmae } = piniaStore
|
||||||
|
|
||||||
|
|
||||||
const handleBtn = () =>{
|
const handleBtn = () =>{
|
||||||
store.commit('getUserNmae', '真乖,如果对您有帮助请在github上点个星星哦~')
|
// vuex
|
||||||
|
// vuexStore.commit('userName','真乖,如果对您有帮助请在github上点个星星哦~')
|
||||||
|
|
||||||
|
// pinia
|
||||||
|
getUserNmae('真乖,如果对您有帮助请在github上点个星星哦~')
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user