前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >(十二)store 之间互相访问

(十二)store 之间互相访问

作者头像
老怪兽
发布2023-02-22 15:45:34
发布2023-02-22 15:45:34
55600
代码可运行
举报
运行总次数:0
代码可运行

一、store 之间互相访问

  • note.js
代码语言:javascript
代码运行次数:0
复制
import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useNoteStore = defineStore('note', () => {
    cosnt noteList = ref([
        {
            title: '标题',
            desc: '详情内容'
        }
    ]),

    return {
        noteList
    }
})
  • user.js
代码语言:javascript
代码运行次数:0
复制
import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useuserStore = defineStore('user', () => {
    cosnt user = ref(
        {
            name: 'bob',
            phone: 15729837802,
            emial: '2966211270@qq.com'
        }
    ),

    return {
        user
    }
})

二、只有当用户登录了才能进行添加

代码语言:javascript
代码运行次数:0
复制
import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useuserStore = defineStore('user', () => {
    cosnt user = ref(
        {
            name: 'bob',
            phone: 15729837802,
            emial: '2966211270@qq.com'
        }
    ),

    function isLoggedIn() {
        return user.value != null
    }

    return {
        user,
        isLoggedIn
    }
})

三、在 note.js 中访问其他 store

  • 获取方法和在组件当中获取是一样的
代码语言:javascript
代码运行次数:0
复制
import { ref } from 'vue'
import { defineStore } from 'pinia'
import { useUserStore } from './user.js'

export const useNoteStore = defineStore('note', () => {
    cosnt noteList = ref([
        {
            title: '标题',
            desc: '详情内容'
        }
    ]),


    const userStore = useUserStore()
    function addNote() {
        if(userStore.isLoggedIn) {
            setTimeout(() => {
                noteList.push({
                    title: '标题x'
                    desc: '详情内容x'
                })
            }, 2000)
        }
    }

    return {
        noteList
    }
})
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年11月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、store 之间互相访问
  • 二、只有当用户登录了才能进行添加
  • 三、在 note.js 中访问其他 store
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档