pinia action axios

2023-09-25  本文已影响0人  糖醋里脊120625
store里面的一个模块
import {defineStore} from "pinia";
import { farmProduceList, } from '@/api/produceApi';
export const productStore = defineStore("productData", {
  state: () => {
    return {
      goodsList: [],
    };
  },
  getters: {
  },
  actions: {
    async getProductDictionary() {
      const resData = await farmProduceList()
      this.goodsList  = resData.data; 
      return this.goodsList
   
    },
async singleMusic() {
      try {
        const res = await fetchRandMusic();
        console.log(res.data)     
        this.umsicInfo =  res.data.data;      
      } catch (error) {
        console.error('获取用户详情失败', error);
      }
    }


    
  },
 
})

export default productStore;

vue文件里面使用
import { productStore } from '@/store/modules/storeProduct';
const productList= ref<GoodsList[]>([]);
const goodsStore = productStore();

const emit = defineEmits([ "postProudctName" ]);
interface GoodsList {
  productName : string;
  id: string;
  unitPrices:string;
  isActive:boolean
}


onMounted(() => {
  getProductDictionary()
 
});

function getProductDictionary(){
  goodsStore.getProductDictionary().then((res:any) => {
    productList.value = res
  });
}

function changeProduct (){
  emit("postProudctName",productName.value);
}
代码块
代码块
上一篇下一篇

猜你喜欢

热点阅读