Pinia 重构过程记录
重构步骤
安装必要依赖
1
npm install pinia
创建 Pinia Store
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25// stores/cvStore.js
import { defineStore } from 'pinia'
import { message } from 'ant-design-vue'
import cvDataStructure from '@/assets/data/cv-info.json'
import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'
export const useCvStore = defineStore('cv', {
state: () => ({
currentLang: 'cn',
activeSections: [],
commentContent: '',
isDownloadingPdf: false,
defaultAvatar: '/images/kkz.jpg',
manualToggleStates: {}
}),
actions: {
// 业务逻辑方法...
},
getters: {
// 计算属性...
}
})修改组件结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<script setup>
import { storeToRefs } from 'pinia'
import { useCvStore } from '@/stores/cvStore'
const store = useCvStore()
const {
currentLang,
activeSections,
isDownloadingPdf,
cvData
} = storeToRefs(store)
// 方法代理
const handleLangChange = ({ key }) => store.handleLangChange(key)
const downloadPDF = () => store.downloadPDF(cvContainerRef.value)
</script>