如何在Vueform中配置Authorization: Bearer请求头并扩展组件代码?
在Vueform中配置Bearer授权请求头的两种方法
方法1:直接使用headers属性
Vueform组件支持通过headers属性直接设置请求头,适用于仅需添加授权头的简单场景:
<Vueform endpoint="https://xxxxx.xx/xxxxx" method="post" :headers="{ 'Authorization': 'Bearer xxxxx' }" > <!-- 表单内容 --> </Vueform>
如果你的token是动态获取的(比如存在localStorage或状态管理中),可以通过模板字符串动态拼接:
<Vueform endpoint="https://xxxxx.xx/xxxxx" method="post" :headers="{ 'Authorization': `Bearer ${localStorage.getItem('authToken')}` }" > <!-- 表单内容 --> </Vueform>
方法2:使用request-options属性
如果需要配置更多Axios请求选项(超时、拦截器等),可以用request-options传入完整的请求配置对象,其中包含headers字段:
<Vueform endpoint="https://xxxxx.xx/xxxxx" method="post" :request-options="{ headers: { 'Authorization': 'Bearer xxxxx' }, timeout: 5000 // 额外配置示例:超时时间 }" > <!-- 表单内容 --> </Vueform>
内容的提问来源于stack exchange,提问作者Naga Vijayapuram
相关产品推荐
相关产品推荐

