当前位置:网站首页>pinia 模块划分

pinia 模块划分

2022-07-06 16:25:00 梁云亮

pinia的模块划分是通过js命名来划分的。

示例:

第一步:拆分store文件

  • store/index.ts
import {
     createPinia } from "pinia"

const store = createPinia()
export default store
  • store/user.ts
import {
    defineStore} from "pinia"

export const useUserStore = defineStore({
        //js命名
    id: "user", 
    state: () => {
    
        return {
    
            name: '张三',
            age: 18
        }
    }
})
  • store/order.ts
import {
    defineStore} from "pinia"

export const useOrderStore = defineStore({
       //js命名
    id: "order", 
    state: () => {
    
        return {
    
            orderList: [{
    id: 1001, total: 6666}, {
    id: 1002, total: 8888}, {
    id: 1003, total: 9999}]
        }
    }
})

第二步:vue

  • User.vue
<template>
  User:{
   { name }} == {
   { age }}
</template>

<script setup lang="ts"> import {
      storeToRefs} from "pinia" import {
      useUserStore} from "@/store/module/user" const userStore = useUserStore() const {
      name, age} = storeToRefs(userStore) </script>
  • Order.vue
<template>
  order <br>
  {
   {order}}
</template>
<script setup lang='ts'> import {
      storeToRefs} from "pinia" import {
      useOrderStore} from "@/store/module/order" const orderStore = useOrderStore() const order = storeToRefs(orderStore) </script>
原网站

版权声明
本文为[梁云亮]所创,转载请带上原文链接,感谢
https://hcshow.blog.csdn.net/article/details/125624599