Vue3/ pinia 完整配置流程

 pinia配置流程

1. 安装

yarn add pinia

2. 创建 store/index.js 在 src 文件下

3. 通过 import 导入 createPinia 函数 

import { createPinia } from 'pinia'

4.调用 createPinia 函数并定义变量 ,最后导出

const pinia = createPinia()

export default pinia

5. main.js 内引入 pinia 并 use 使用

import { createApp } from 'vue'

import App from './App.vue'

import pinia from './stores/index.js'

createApp(App).use(pinia).mount('#app')

6. 创建 store/modules/counter.js  在 src 文件下

7. 通过 import 从 pinia 内 导入 defineStore

import { defineStore } from 'pinia'

8.调用defineStore函数并定义变量名 , 然后export default 导出

const useCounterStore = defineStore ()
 

export default useCounterStore

9. defineStore插入 参数 名称,名称 是必传的 参数是 string , 然后插入 pinia 三大天王

const useCounterStore = defineStore (‘counter’,{

    state:()=>{

    name: 'hello World Pinia'

    },

   getters:{},

   actions:{}

})

10. 统一导出 counter.js 文件 在 store/index.js 内

import { createPinia } from 'pinia'

const pinia = createPinia()

export default pinia

/* 统一导出 */

export * from './modules/counter.js'

11. Vue文件使用pinia

注意:

  • pinia内没有 mutations  和 modules
  •  Vue文件内从 pinia/index.js 导入 useCounterStore 并注册

<template>
{{ store.name }}
</template>

<script setup>
import { useCounterStore } from '@/store/index.js'
const store = useCounterStore()
</script>

<style lang="scss" scoped></style>

a: 使用 state

state作用: 存储

(1) state内定义变量

const useCounterStore = defineStore (‘counter’,{

    state:()=>{

    return{

     num: '你好世界'

    }

   },

   getters:{},

   actions:{}

})

(2) Vue文件内使用

<template>
{{ store.num}}
</template>

<script setup>
import { useCounterStore } from '@/store/index.js'
const store = useCounterStore()
</script>

b:使用 getters

getters作用: 计算属性

(1) getters内定义

const useCounterStore = defineStore (‘counter’,{

    state:()=>{

    return{

     num: 10

    }

   },

   getters:{

    count: state => { 

     return state.num + 50

    }

   },

   actions:{}

})

(2) vue文件内使用

<template>
{{ store.count}}
</template>

<script setup>
import { useCounterStore } from '@/store/index.js'
const store = useCounterStore()
</script>

c: 使用 actions 

actions作用: 修改存储数据 包括同步和异步

注意: actions内 是通过 this 去访问 state 内数据

(1) 在actions内定义

const useCounterStore = defineStore (‘counter’,{

    state:()=>{

    return{

     num: 10

    } 

   },

   getters:{

    count: state => { 

     return state.num + 50

    }

   },

   actions:{

   amend(){

    this.num = '我被修改了' 

  }

  }

})

(2) Vue文件内使用

<template>
<button  @click="update"> 点我使用 actions修改 state内数据 </button>
</template>

<script setup>
import { useCounterStore } from '@/store/index.js'
const store = useCounterStore()

const update = () => { 

  state.amend()

}
</script>


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值