Project scenario:

As is shown in

Explanation: This is a common multi-condition query service, generally is to pass the corresponding parameters, search the corresponding list data


Problem description

The backend now says: only when there is a value in the drop-down or input box, the corresponding field and value will be passed over, otherwise, do not pass the field over!

As shown in figure:

Let’s look at the part of the code that passed the parameter:

1.At the beginning of the calculation of the property, considering the common time easy to handle, but also reduce the code, to pass the time directlythis. Params_search can2.However, the downside is obvious. The data structure is already in place, and a lot of writing is done in params_search to fulfill the initial requirementsifJudging, very uncomfortable. Do not write and not line, directly passed, and there will be some backend do not fieldcomputed: {
    params_search() {
      return {
        prj_level: this.selectData.value_level,
        prj_name: this.selectData.prj_name,
        prj_city: this.value_area[0]?this.value_area[0] : "".prj_county: this.value_area[1]?this.value_area[1] : "".opportunity_id: this.selectData.opportunity_id,
        page: this.currentPage,
        page_size: this.pagesize, }; }},Copy the code

Solution:

Code to solve code, magic to defeat magic ~

Since you can’t touch the above data structure, you can simply create a new one, and then pass it to the back end when the query sends the request

Request code:

handleInit() {
      letobj = {}; Define an empty objectfor (const key in this.params_search) {get the value of the original data, i.e. the value of the form componentlet tem = this.params_search[key]; Judge the data type a little bit0and1In the caseif (typeof tem == "number") {
          obj[key] = tem;
        } else if (typeof tem == "string") {
          if (tem.length > 0) { obj[key] = tem; }}}// console.log(obj); You can print it out yourself and see if it's the data structure you need
      this.$axios({
        url: "your url".method: "POST".responseType: "json".data: obj,
      })
        .then((data) = >{})},Copy the code

Conclusion:

Writing this, I burst into tears, after all, too much coddling of this backend…