【发布时间】:2021-12-03 00:16:44
【问题描述】:
我的脚本我正在使用 axios 和 vuex,但有必要在脚本中将 formData 更改为 Json,并且它从 POST/loginB2B 200 api 返回,但它不会插入到本地存储中所以它不会指向仪表板页面。
**Auth.js**
import axios from "axios";
const state = {
user: null,
};
const getters = {
isAuthenticated: (state) => !!state.user,
StateUser: (state) => state.user,
};
async LogIn({commit}, user) {
await axios.post("loginB2B", user);
await commit("setUser", user.get("email"));
},
async LogOut({ commit }) {
let user = null;
commit("logout", user);
},
};
**Login.vue**
methods: {
...mapActions(["LogIn"]),
async submit() {
/*const User = new FormData();
User.append("email", this.form.username)
User.append("password", this.form.password)*/
try {
await this.LogIn({
"email": this.form.username,
"password": this.form.password
})
this.$router.push("/dashboard")
this.showError = false
} catch (error) {
this.showError = true
}
},
},
app.vue
name: "App",
created() {
const currentPath = this.$router.history.current.path;
if (window.localStorage.getItem("authenticated") === "false") {
this.$router.push("/login");
}
if (currentPath === "/") {
this.$router.push("/dashboard");
}
},
};
api /loginB2B 返回 200,但它没有创建存储以重定向到仪表板。
我使用这个例子,但是我需要传递 json 而不是 formData: https://www.smashingmagazine.com/2020/10/authentication-in-vue-js/
【问题讨论】:
-
您说它不会在 localStorage 中存储任何内容,但我也没有看到您尝试在代码中存储任何内容。我有点不清楚为什么你认为它现在应该在那里存储一些东西。
-
我插入了我的 app.vue 以显示代码从本地存储中获取值。