requestInterceptors.js 617 B

1234567891011121314151617
  1. /**
  2. * 请求拦截
  3. * @param {Object} http
  4. */
  5. module.exports = (vm) => {
  6. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  7. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  8. config.data = config.data || {}
  9. config.header.Authorization = vm.$store.state.token//设置请求token
  10. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  11. console.log(vm.$store.state.token);
  12. return config
  13. }, (config) => // 可使用async await 做异步操作
  14. Promise.reject(config))
  15. }