Get the return value of this.$
This.$() is used to pass the value to vuex to change state.
Let's see how to get the return value of this.$() call.
Action
First look at the defined Action:
login({ commit }, userInfo) { // const { username, password } = userInfo return new Promise((resolve, reject) => { login(userInfo).then(response => { const { data } = response commit('SET_TOKEN', ) setToken() resolve(response) }).catch(error => { reject(error) }) }) },
Two key points:
- Return a new Promise
return new Promise((resolve, reject)
- The return value is passed in the resolve function
resolve(response)
Call
this.$('user/login', ) .then(res => { (res) (); //Login to the home page or the address you need to redirect before accessing this.$({ path: || '/' }) = false }) .catch(error => {}
In the call, you can directly obtain the return value through res.
.then(res => { (res)
According to vuex's this.$() return value processing logic
const ret = await this.$('userLogin', { username: , password: }) if (ret && ) { this.$() await this.$('controlLoginDialog', false) } else { this.$(ret) }
vuex/store/
async userLogin ({commit}, account) { let userInfo = {} return new Promise((resolve, reject) => { requestUserLogin(account).then(response => { if ( === 200) { if () { userInfo = = = true resolve({ info: + 'Login successfully, welcome to Baidu Cloud Intelligence Academy Experimental Platform' }) } else if () { = '' = false myConsole('') resolve() } } else { = '' = false } commit(USER_LOGIN, {userInfo}) }).catch(err => { myConsole(err) reject(err) }) }) },
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.