实现效果:
根据pinia中存储的不同状态,
点击不同的按钮,切换不同的弹窗和标题
1. Pinia Store(组合式写法)
import {
defineStore } from 'pinia'
import {
reactive } from 'vue'
type DialogType = 'type1' | 'type2'
interface DialogState {
id: number
name: string
visible: boolean
}
export const useDataStore = defineStore('dataType', () => {
const state = reactive({
dialogs: {
type1: {
id: 1, name: '数据1', visible: false },
type2: {
id: 2, name: '数据2', visible: false }
} as Record<DialogType, DialogState>,
showDownloadList: false
})
const toggleDialog = (type: DialogType, visible: boolean) => {
Object.keys(state.dialogs).forEach(key => {
state.dialogs[key as DialogType].visible = false
})
state.dialogs[type].visible = visible
}
return {
...toRefs(state),
toggleDialog
}
})
2. 按钮组件(组合式写法)
<!-- Buttons.vue -->
<template>
<div class="header-box">
<el-button type="primary" size="large" @click="openDialog('type1')">
类型弹窗1
</<