外壳API
//所有的接口的通用类型
export interface IApiRes<T>{
code : string,
msg:string,
result: T
}
单个分类的类型
export type 详细数据泛型 = {
获取数据的类型
}
src -> types -> data.d.ts文件里设置
//1.导入菠萝 - pinia
import { defineStore } from "pinia"
//导入axios -- request
import request from '@/utils/request'
//导入泛型
import {详细数据泛型,API外壳} from '@types/data'
export default defineStore('导出方法名',{
state: () => ({
//数据储存
数据存储: [] as 详细数据泛型[]
}),
actions:{
//请求
async get请求List() {
const res = await request.get<API外壳<详细数据泛型[]>>('请求路径')
this.数据存储 = res.需要的数据
}
}
})
导出方法名 = 就是 store 的唯一一个的id ,因为有id的存在,所有可以创建多个模块
src -> store -> modules -> home.ts
中间键 / index.ts
src -> store -> index.ts
import home from "./modules/home";
export default function useStore(){
return {
home:home()
}
}
调用方法
import useStore from '@/store'
//解构
const { home } = useStore
home.get请求List()