Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

vue 使用this.$store.dispatch 如何传递 一个对象和一个回调函数

登录功能 在action中发送请求成功后触发回调函数 提示信息
这样能否可行
vue

let param = this.loginFrom;
 this.$store.dispatch('getUserInfo',param, function(result) {
            console.log("来到回调函数");
            if (result.data.code) {
              this.loading = false;
              this.$router.replace('/home')
              this.$message({
                type:'success',
                message:'登陆成功'
              });
            } else {
              this.loading = false;
              this.$message({
                type:'error',
                message:result.data.msg
              });
            }
          });

action

async getUserInfo({commit},  param ,callback){
        if(param.role === '0'){ //老师身份登录
            const teacher = {
                teacherName: param.username,
                password: param.password,
            };
            const result = await TeacherLogin(teacher);
            if (result.data.code) {
                const userInfo = result.data.datas;
                commit(RECEIVE_USER_INFO,{userInfo})
                console.log("接下来执行回调函数");
                callback && callback(result);
            } 
        }     
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Promise

async getUserInfo({commit},  param ){
            return new Promise(resolve =>{
                if(param.role === '0'){ //老师身份登录
            const teacher = {
                teacherName: param.username,
                password: param.password,
            };
            const result = await TeacherLogin(teacher);
            if (result.data.code) {
                const userInfo = result.data.datas;
                commit(RECEIVE_USER_INFO,{userInfo})
                console.log("接下来执行回调函数");
               resolve(resolve)
            } 
        } 
            })
    },
 this.$store.dispatch('getUserInfo',param).then(result => {
         console.log("来到回调函数");
            if (result.data.code) {
              this.loading = false;
              this.$router.replace('/home')
              this.$message({
                type:'success',
                message:'登陆成功'
              });
            } else {
              this.loading = false;
              this.$message({
                type:'error',
                message:result.data.msg
              });
            }
          }
 })

回调

let param = this.loginFrom;
const callback = function(result) {
            console.log("来到回调函数");
            if (result.data.code) {
              this.loading = false;
              this.$router.replace('/home')
              this.$message({
                type:'success',
                message:'登陆成功'
              });
            } else {
              this.loading = false;
              this.$message({
                type:'error',
                message:result.data.msg
              });
            }
          }
 this.$store.dispatch('getUserInfo',{callback, param});
async getUserInfo({commit},  {param ,callback}){
        if(param.role === '0'){ //老师身份登录
            const teacher = {
                teacherName: param.username,
                password: param.password,
            };
            const result = await TeacherLogin(teacher);
            if (result.data.code) {
                const userInfo = result.data.datas;
                commit(RECEIVE_USER_INFO,{userInfo})
                console.log("接下来执行回调函数");
                callback && callback(result);
            } 
        }     
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...