gpt4 book ai didi

reactjs - 在 react 中导出常量

转载 作者:行者123 更新时间:2023-12-02 03:34:02 25 4
gpt4 key购买 nike

大家好,我如何导出我的 const history 以在另一个组件中使用相同的历史记录。我尝试使用此历史记录在其他 View 和组件中使用 history.push

export function login(data) {

const history = createBrowserHistory({
forceRefresh: true
})

const endpoint = '/api/auth/jwt/'
const csrfToken = cookie.load('csrftoken')
let thisComp = this
if (csrfToken !== undefined) {
let lookupOptions = {
method: "POST",
headers: {
'Content-Type': 'application/json'

},
body: JSON.stringify(data),
credentials: 'include'
}

fetch(endpoint, lookupOptions)
.then(function(response){
return response.json()
}).then(function(responseData){
console.log(responseData)
localStorage.token = responseData.token
localStorage.expires = responseData.expires // Store the token
console.log("Token Stored", localStorage.token)
console.log("Token Expires", responseData.expires)
refreshToken(localStorage.token)
history.push('/meals/')


}).catch(function(error){

console.log("error", error)
})
}

}

谢谢你的帮助。

最佳答案

您应该将您的常量定义移到登录之外并将其导出,如下所示

export const history = createBrowserHistory({
forceRefresh: true
})

export function login(data) { ... }

然后就可以通过这种方式从外部导入

import { history } from 'pathToTheFile';

关于reactjs - 在 react 中导出常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892667/

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