分享心得:vue接口调用赋值

写的远远不够好,但是在一点点进步吖      < _ >

刚开始写接口调用的时候,只知道在data里声明一个变量,async 搭配await调接口拿回数据,通过code的判断,就直接把数据赋值给data的变量了

现在,会进行一轮又一轮的判断同时学会了使用局部变量进行控制,看代码👇

以前的代码:

async StatisticsPlanChartStatus() {
      const search = {
        startTime: this.search.startTime
          ? `${this.search.startTime} 00:00:00`
          : null,
        endTime: this.search.endTime ? `${this.search.endTime} 23:59:59` : null,
      };
      const { code, data = [] } = await StatisticsPlanChartStatusApi(search);
      if (code === 200 || code === "200") {
        this.option2.series[1].data = data.map((item) => ({
          value: item.count,
          name: item.value,
        }));
        this.echarts2.setOption(this.option2);
      } else this.echarts2.clear();
    },

现在的代码:

async StatisticsPlanChartStatus() {
      const search = {
        startTime: this.search.startTime
          ? `${this.search.startTime} 00:00:00`
          : null,
        endTime: this.search.endTime ? `${this.search.endTime} 23:59:59` : null,
      };
      //   局部变量
      const seriesData = [];
      const { code, data = [] } = await StatisticsPlanChartStatusApi(search);
      if (code === 200 || code === "200") {
        if (data && data.length) {
          for (const item of data) {
            //   局部变量赋值
            seriesData.push({
              value: item.count,
              name: item.value,
            });
          }
        }
      }
      if (seriesData && seriesData.length) {
        //   全局变量赋值
        this.option2.series[1].data = seriesData;
        this.echarts2.setOption(this.option2);
      } else this.echarts2.clear();
    },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 接口调用顺序执行的方式有多种,以下是其中的一些示例: 1. 使用 async/await 可以在 Vue 的 methods 中使用 async/await,确保按顺序执行异步方法。例如: ```javascript methods: { async fetchData1() { // 异步获取数据的方法1 }, async fetchData2() { // 异步获取数据的方法2 }, async fetchData() { await this.fetchData1(); await this.fetchData2(); // 执行完 fetchData1 后再执行 fetchData2 } } ``` 2. 使用 Promise 可以将异步方法封装成 Promise,再使用 Promise 的链式调用,确保按顺序执行。例如: ```javascript methods: { fetchData1() { return new Promise(resolve => { // 异步获取数据的方法1 resolve(); }); }, fetchData2() { return new Promise(resolve => { // 异步获取数据的方法2 resolve(); }); }, fetchData() { this.fetchData1().then(() => { return this.fetchData2(); }).then(() => { // 执行完 fetchData1 后再执行 fetchData2 }); } } ``` 3. 使用 async/await 和 Promise 的结合 使用 async/await 和 Promise 的结合,可以更方便地写出顺序执行的代码。例如: ```javascript methods: { fetchData1() { return new Promise(resolve => { // 异步获取数据的方法1 resolve(); }); }, fetchData2() { return new Promise(resolve => { // 异步获取数据的方法2 resolve(); }); }, async fetchData() { await this.fetchData1(); await this.fetchData2(); // 执行完 fetchData1 后再执行 fetchData2 } } ``` 以上是几种常用的 Vue 接口调用顺序执行的方式,可以根据具体的需求选择适合自己的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值