鼠标点击按钮请求axios获取不到更改的值?

按钮点击页面

window.init = {};
var initValue = null;
Object.defineProperty(init, "timestamp_to_date",{
    configurable: true,  
    enumerable: true,
    get:function (){
        //当获取值的时候触发的函数
        return initValue;
    },
    set:function (value){
        //当设置值的时候触发的函数,设置的新值通过参数value拿到
        initValue = value;
    }
});

点击事件更改值

 el.onclick = funcrion () {
 init.timestamp_to_date = Date.now() + `web${window.token}.plugin`
 }

axios 页面,封装请求并赋值点击事件取到的timestamp_to_date

axios.interceptors.request.use((config) => {
    let timestamp = window.init;
    config.headers.token = timestamp.timestamp_to_date
    if (!request) return config;
    return request(config);
  }, (error) => {
    //  替换错误信息
    let errorData = this.replaceMessage(error);
    return errorFn(errorData);
  });

然后我打印得到是空对象,里面还有值,然后请求头token还是null,总感觉axios.request是先走了,请问如何解决
image.png

阅读 2k
2 个回答

request interceptor 应该 return config 本身吧…你的 request(config) 是什么作用?

你的ajax怎么触发的?是不是点击事件在发送ajax之后才触发的?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题