feat:pinia初探

This commit is contained in:
talktao 2022-04-11 22:58:40 +08:00
parent e52d8af1cf
commit 1b5844759b
4 changed files with 57 additions and 12 deletions

View File

@ -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')

View File

@ -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
View 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
// }
// }
// })

View File

@ -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)
// storeToRefspiniastate
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>