数据请求:store请求

这种是写在actions里面的

写这种请求要注意

写请求是一个步骤,写引入是一个步骤,写使用又是一个步骤 接下来请看:

一、写请求
import { updateState } from "@/utils/appFunc";
import { Request } from "@/utils/xhr";

export default {
  namespaced: true,
  state: {
  },
  mutations: {
    updateBillHallState: updateState,
  },
  actions: {
    async addCounterfoil ({ state }, params) {
      return await Request.fetch({ url: '/api/counterfoil/addCounterfoil', method: 'POST', ...params });
    },
    async countRemainingDays ({ state }, params) {
      return await Request.fetch({
        url: '/api/counterfoil/countRemainingDays', method: 'GET', ...params
      });
    },
}
 modules: {
  }
}
二、写引入
 ···········································这个是用传值的方式,但是注意,请求 和 方法名    (不可同名)
  

 ···········································在你需要使用的地方引入
methods: {
    ...mapActions('billHall', [
      'getBillList',
      'getPlatform',
      'getContract',
      'getDetailShowId',
    ]),
    async getList(params) {
      return await this.getBillList(params)
    },
    async getPlatforms(params) {
      // console.log(params)
      return await this.getPlatform(params)
    },
    async getContracts(params) {
      return await this.getContract(params)
    },
    async getDetailShowIds(params) {
      return await this.getDetailShowId(params)
    },
  },
三、写使用
 props: {
    getPlatforms: {
      // 获取表格数据action方法
      type: Function,
      default: () => new Promise((resolve) => resolve({ data: [] })),
    },
}
methods:{
  platformChange() {
      // console.log('平台合同')
      this.getPlatforms({
        params: {},
      }).then((res) => {
        if (res.code == 0) {
          // console.log(res)
          this.contractList = res.data
        }
      })
    },
}
························································也可以写在created()里
created() {
    this.getPlatforms({
      params: {},
    }).then((res) => {
      if (res.code == 0) {
        // console.log(res)
        this.contractList = res.data
      }
    }),
}

你可能感兴趣的:(数据请求:store请求)