problem description
I just started using the vue write function, and the data in loginStore is empty. I wonder if I can save the userdata in api.js into store!
related codes
loginStore.js
import {
login
} from "../Services/api.js";
import {
to
} from "await-to-js";
import {
resolve
} from "path";
export default {
state: {
username: "admin",
password: "123456",
userdata: ""
},
mutations: {
ADD_LOGIN_USER_INFO(
state, data
) { //sessionStorage
sessionStorage.setItem("userdata", JSON.stringify(data));
state.userdata = data;
},
},
actions: {
async login({
state,
commit,
}) {//
let data = await to(
login.handleLogin({
userCode: state.username,
userPassword: state.password
})
);
if (state.username == null || state.username == "" || state.username.replace(/(^\s*)|(\s*$)/g, "") == "") {
alert("");
return;
} else if (state.password == null || state.password == "" || state.password.replace(/(^\s*)|(\s*$)/g, "") == "") {
alert("");
return;
}
//,return,
console.log(JSON.stringify(data));
commit("ADD_LOGIN_USER_INFO", data);
}
},
getters: {
}
}
loginStore the data typed here is [null,null]
api.js
import axios from "axios";
import Qs from "qs";
import store from "../store/loginStore"
let userdata;
export const login = {
//
async handleLogin() {
let postData = Qs.stringify({
userCode: store.state.username,
userPassword: store.state.password
});
return axios({
method: "post",
url: "/login/loginVerify",
data: postData
}).then((res) => {
if (res.data.code == 1) {
userdata = res.data.result;
console.log(userdata) //
alert("")
} else {
alert("!")
return;
}
}).catch(function (error) {
console.log(error);
});
},
}
result
I think it"s worth it in data, so you don"t have to do userdata in api.js
.