gpt4 book ai didi

javascript - Nuxt JS 中间件不能使用 axios

转载 作者:行者123 更新时间:2023-12-04 07:51:14 25 4
gpt4 key购买 nike

我想创建一个中间件来检查 token 是否有效,但 axios 无法读取。任何人都可以帮助我如何找到解决方案。这是我的代码:

中间件/authentication.js

export default  ({ app, redirect }) => {

let data = {
'userid': app.$cookies.get('user_id'),
'token': app.$cookies.get('auth_token'),
};

app.$axios.post(`/tokenVerify`,data)
.then(res => {
console.log(res)
})
}

pages/index.vue

    export default {
middleware: "authentication",

nuxt.config.js

  modules: [
'@nuxtjs/axios',
],
axios: {
proxy: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Accept' : 'application/json, text/plain, */*',
'Access-Control-Allow-Methods' : 'GET, PUT, POST, DELETE, OPTIONS',
'Access-Control-Allow-Credentials' : true,
}
},

proxy: {
'/tokenVerify':{target:process.env.BASE_URL+'index.php/public/tokenVerify', pathRewrite: {'^/tokenVerify': ''}, changeOrigin: true},
'/vimeoEmbed':{target:'https://vimeo.com/api/oembed.json', pathRewrite: {'^/vimeoEmbed': ''}, changeOrigin: true},
},




最佳答案

您将 $axios 服务注入(inject)到中间件上下文参数中。所以你应该能够像这样访问它:

export default  ({ app, redirect, $axios }) => {
let data = {
'userid': app.$cookies.get('user_id'),
'token': app.$cookies.get('auth_token'),
};

$axios.post(`/tokenVerify`,data)
.then(res => {
console.log(res)
})
}

关于javascript - Nuxt JS 中间件不能使用 axios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66964100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com