方法:
import axios from "axios";
import Cookies from "js-cookie";//cookie插件
//token封装
const setAuthToken = (token) => {
if (Cookies.get("jwtToken") && Cookies.get("jwtToken") !== "") {
token = Cookies.get("jwtToken");
}
axios.defaults.headers.common["token"] = token;
};
export default setAuthToken;
登录成功存token:
//储存token到Cookies
const { token } = res.data.data;
Cookies.set("jwtToken", token);
引用路径:
import setAuthToken from "../../components/setAuthToken"; //token封装
每个页面调用token:
componentDidMount() {
setAuthToken();
}