在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue如何靈活設(shè)置不同頁面不同的limit參數(shù)

vue如何靈活設(shè)置不同頁面不同的limit參數(shù)

利用vue-cli + ElementUI 隨手寫個(gè)東西

然后根據(jù)網(wǎng)上的說法,簡單封裝了下axios,設(shè)置了一個(gè)攔截器,一個(gè)request,一個(gè)response

如果我所以接口都是

limit = 50

我可以在request 里 設(shè)置

config.params['limit'] = 50;

但是我有些頁面不能都是50,這樣可能數(shù)據(jù)就太多了...

所以我想 設(shè)置部分的是20,一部分可能是10,大部分還是50,

然而我并不想每個(gè)頁面請求的時(shí)候都帶一個(gè) limit=10 這種寫法,

那么,請問各位大神,我該怎么寫?

下面是我封裝axios的代碼

import axios from 'axios';
import { Message } from 'element-ui';
import cookie from '../utils/cookie.js';


const service = axios.create({
  baseURL: process.env.BASE_API, 
  timeout: 5000,                  
  params:{}
});


service.interceptors.request.use(config => {
// console.log('請求前');
  if (config.method === 'get') {
    config.params['limit'] = 50;
  }


  if (cookie.getCookie('token') ) {
    config.headers['x-auth-token'] = cookie.getCookie('token'); 
  }

  return config;
}, error => {
  console.log(error); 
  Promise.reject(error);
})

// respone攔截器
service.interceptors.response.use(function (response) {
    // console.log('請求后',this.$route.params);

    // console.log(response.data);
    const code = response.data.code;
    const message = response.data.message;
    if (code === 411000000) {
      location.href = '/login'
    }
    if (code !== 0 && code !== 200 ) {
        Message({
          message: message,
          type: 'error',
          duration: 5 * 1000
        });
    }
    return response;
},
 
  error => {
    console.log('err' + error);
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    });
    return Promise.reject(error);
  }
);

export default service;
回答
編輯回答
舊酒館

if(!config.params['limit']){

config.params['limit'] = 50;

}

2017年12月6日 00:40
編輯回答
局外人

在transformRequest里封裝一下,判斷一下params里有沒有l(wèi)imit這個(gè)參數(shù),有就用傳過來的,沒有就用默認(rèn)的

2018年4月6日 04:26
編輯回答
雨萌萌

建議我這樣封裝
import Qs from 'qs';
export default{
url: '/route',
baseURL: 'http://0.0.0.0:8080',
method: 'POST',
transformRequest: [function(data) {

// 為了避免qs格式化時(shí)對內(nèi)層對象的格式化先把內(nèi)層的對象轉(zhuǎn)為
// data.CustData = JSON.stringify(data.CustData);
// 由于使用的form-data傳數(shù)據(jù)所以要格式化
data = Qs.stringify(data);
return data;

}],
transformResponse: [function(data) {

return data;

}],
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
params: {

limit: 10,
openid: '57c80528e4b07e300ed4ffbb',
pubacckey: '',
timestamp: '',
nonce: '',
pubaccid: ''

},
paramsSerializer: function(params) {

return Qs.stringify(params);

},
data: {},
timeout: 1000,
withCredentials: false, // default
responseType: 'json', // default
maxContentLength: 2000,
validateStatus: function(status) {

return status >= 200 && status < 300; // default

},
maxRedirects: 5 // default
};

2018年4月21日 02:51