组件的api请求有两个入参是不同的,为了复用,想做成组件,求问,这个值怎么能是动态的?

问题如下:有个请求是一模一样的代码,后台返回的数据也是一样的格式,唯一不同的是有个入参不同,一个是0,一个是1,为了复用,想把这块功能做成组件,求问,传的这个report_type和id值,怎么写成动态的?

clipboard.png

clipboard.png

阅读 2.2k
2 个回答

组件更多的是展示页面的组成,这个最好做成一个通用的service会比较好吧。

//service.js
export async function getUrlByReport (report_type,id) {
  const sendData = {
    report_type,
    id,
    index_x: -1,
    index_y: -1
  }
  const res = await urlGetReporter(sendData);
  return res;
}
//index.vue

import {getUrlByReport} from './service.js'



const data = await getUrlByReport(0,this.$route.params.id)
const data = await getUrlByReport(1,this.$route.params.id)  

加个 props: ['reportType'], 使用的时候 this.reportType就好

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题