【vue3】學習對store中資料的使用

清清飞扬發表於2024-03-08

src/store/modules/nav.js

import {defineStore} from 'pinia';
import {handleTree} from '@/utils/ruoyi'
import {list} from "@/api/nav/node";

const store = defineStore(
	'nav',
	{
		state: () => ({
			nodeList: []
		}),
		actions: {
			GetTree() {
				return new Promise((resolve, reject) => {
					list().then(res => {
						this.nodeList = handleTree(res.data, 'id');
						resolve()
					}).catch(error => {
						reject(error)
					})
				})
			},
		}
	})

export default store

  xx.vue:使用

經過computed處理過後的nodeList才是響應式資料,而且會監視著store中nodeList的變化的

相關文章