vuex—actions
store.js
import Vue from "vue"
import Vuex from "vuex"
import { Login } from "@/api/login"
Vue.use(Vuex)
export default new Vuex.Store({
state:{
},
getters:{
},
mutations:{
},
actions:{
login(context, requestData) { //参数一:context 参数二:传的参数
return new Promise((resolve, reject) => { //new Promise({ }) resolve,reject分别是两个函数
//接口
Login(requestData).then(response => {
resolve(response) //异步成功时调用resolve()函数,并将请求结果 response 返回出去。
}).catch(error => {
reject(error)
})
})
}
})
Login.vue vue3.0语法
const login = () => {
const requestData = {
username:ruleForm.username,
password:sha1(ruleForm.password),
code:ruleForm.code
}
root.$store.dispatch("login",requestData).then(response => {
console.log(response);
root.$router.push("/console")
})
}