axios等请求中catch说明

2019-05-28  本文已影响0人  小丘啦啦啦

then中处理数据、调用其他方法,如果抛错的话,这些错误会被catch到,并执行里面的内容。

  getRely(applicationVersionId) {
      let _this = this;
      _this
        .axios({
          url: _this.config.apiServer + "/msmall/application/listRely",
          method: "post",
          data: {
            applicationVersionId: applicationVersionId
          }
        })
        .then(res => {
          console.log(res);
          const resData = res.data;
          if (
            resData.returnCode !== undefined &&
            "0000" != resData.returnCode
          ) {
            _this.common.thisError(
              `错误信息[ ${resData.returnCode} + ${resData.message}] `
            );
            console.log(
              `错误信息:${
                resData.returnCode
              } + 接口/msmall/application/listRely`
            );
          } else {
            if (resData.dataSetResult[0].data.data.length == 0) {
              this.drawData = false;
            } else {
              this.drawData = true;
            }
            _this.drawRelationChart(resData.dataSetResult[0].data);   //加载echart图表
          }
        })
        .catch(function(error) {
          _this.common.thisError(error);
          console.log(`请求错误:${error} + 接口/msmall/application/listRely`);
        });
    }
  drawRelationChart(param) {
      this.relationChart = echarts.init(
        document.getElementById("transferRelation")
      );
      this.relationChart.setOption({
        color: [
          "#ff6666",
          "#ffe966",
          "#92ff66",
          "#66ffbd",
          "#66bdff",
          "#9266ff",
          "#ff66e9"
        ],
        title: {
          text: ""
        },

        animationDurationUpdate: 1500,
        animationEasingUpdate: "quinticInOut",
        series: [
          {
            type: "graph",
            layout: "none",
            focusNodeAdjacency: true,
            symbol: "rect",
            symbolSize: [100, 32],
            roam: true,
            label: {
              normal: {
                show: true
              }
            },
            edgeLabel: {
              normal: {
                show: true,
                textStyle: {
                  fontSize: 20
                },
                formatter: "{c}"
              }
            },
            edgeSymbol: ["rectangle", "arrow"],
            edgeSymbolSize: [0, 10],
            data: param.data,
            links: param.link,
            categories: [
              {
                name: "调用",
                itemStyle: {
                  normal: {
                    color: "#ff6666"
                  }
                }
              }
            ],
            lineStyle: {
              normal: {
                opacity: 0.9,
                width: 1,
                curveness: 0
              }
            }
          }
        ]
      });
    },

此处axios向后台发送请求,then()接收数据,然后调用_this.drawRelationChart()进行数据处理,加载echart图表。如果没有引入echart,则_this.drawRelationChart()会抛错,此错误会被getRely()方法中的catch抓到,同时执行里面的内容,虽然它本身的请求是成功的。


请求已成功,then()中调用其他方法。

进入到此方法执行(这里没有引入echart),会抛错被catch到。

控制台打印:
上一篇下一篇

猜你喜欢

热点阅读