Welcome to OGeek Q&A 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 Reply

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);
            } 
        }     
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...