美文网首页
pinia action axios

pinia action axios

作者: 糖醋里脊120625 | 来源:发表于2023-09-25 15:04 被阅读0次
    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);
    }
    
    代码块
    
    代码块
    

    相关文章

      网友评论

          本文标题:pinia action axios

          本文链接:https://www.haomeiwen.com/subject/vnqcbdtx.html