在login.vue的methods有一个刷新验证码的方法,在另外一个JS里我想要引用那个刷新验证码的方法,应该怎么写才对?login.vue文件登入importmomentfrom'moment'exportdefault{name:'Login',data(){constvalidateUsername=(rule,value,callback)=>{if(value.length===0){callback(newError('请输入用户名'))}else{callback()}}constvalidatePass=(rule,value,callback)=>{if(value.length===0){callback(newError('请输入密码'))}else{callback()}}constvalidateVerify=(rule,value,callback)=>{if(value.length!==4){callback(newError('请输入正确的验证码'))}else{callback()}}return{loginForm:{username:'',password:'',verifyCode:''},loginRules:{username:[{required:true,trigger:'blur',validator:validateUsername}],password:[{required:true,trigger:'blur',validator:validatePass}],verifyCode:[{required:true,trigger:'blur',validator:validateVerify}]},verifyUrl:'',verifyImg:process.env.BASE_API+'/base/getVerify',loading:false,pwdType:'password',redirect:undefined}},watch:{$route:{handler:function(route){this.redirect=route.query&&route.query.redirect},immediate:true}},created(){this.verifyUrl=this.verifyImg},methods:{refreshVerify(){console.log(this.verifyUrl)this.verifyUrl=this.verifyImg+'?v='+moment().unix()},showPwd:function(){if(this.pwdType==='password'){this.pwdType=''this.$refs.showpwd.innerHTML=''}else{this.pwdType='password'this.$refs.showpwd.innerHTML=''}},handleLogin(){this.$refs.loginForm.validate(valid=>{if(valid){this.loading=truethis.$store.dispatch('Login',this.loginForm).then(()=>{this.loading=falsethis.$router.push({path:this.redirect||'/'})}).catch(()=>{this.loading=false})}else{console.log('errorsubmit!!')returnfalse}})}}}$bg:#2d3a4b;$light_gray:#666;/*resetelement-uicss*/.login-container{.el-input{display:inline-block;height:47px;width:80%;input{background:transparent;border:0px;-webkit-appearance:none;border-radius:0px;padding:12px5px12px15px;color:$light_gray;height:47px;&:-webkit-autofill{-webkit-box-shadow:000px1000px$bginset!important;-webkit-text-fill-color:#fff!important;}}}.el-form-item{border:1pxsolid#d2d2d2;border-radius:5px;color:#454545;}}$bg:#f2f2f2;$dark_gray:#889aa4;$light_gray:#eee;.login-container{position:fixed;height:100%;width:100%;background-color:$bg;.login-form{position:absolute;left:0;right:0;width:375px;max-width:100%;padding:35px35px15px35px;margin:120pxauto;background-color:#fff;border-radius:10px;}.tips{font-size:14px;color:#fff;margin-bottom:10px;span{&:first-of-type{margin-right:16px;}}}.svg-container{padding:6px5px6px15px;color:$dark_gray;vertical-align:middle;width:30px;display:inline-block;}.title{font-size:26px;font-weight:400;color:$light_gray;margin:0pxauto15pxauto;text-align:center;font-weight:bold;}.show-pwd{position:absolute;right:10px;top:7px;font-size:16px;color:$dark_gray;cursor:pointer;user-select:none;}.verify{display:inline-block;width:150px!important;}.verify.el-input{width:75%;}.verify-pos{display:inline-block;width:150px;vertical-align:middle;}.verify-posimg{cursor:pointer;}}request.js//response拦截器service.interceptors.response.use(response=>{constres=response.dataif(res.code!==200){Message({message:res.error,type:'error',duration:3*1000})if(res.code===50008||res.code===50012||res.code===50014){MessageBox.confirm('登录超时,请重新登录','确定登出',{confirmButtonText:'重新登录',cancelButtonText:'取消',type:'warning'}).then(()=>{store.dispatch('FedLogOut').then(()=>{location.reload()})})}/*我想在这里写入刷新验证码的操作*/returnPromise.reject('error')}else{returnresponse.data}},error=>{console.log('err'+error)Message({message:error.error,type:'error',duration:3*1000})returnPromise.reject(error)})
2 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
在js里importloginfrom'./login.vue';注意from后面的路径必要时,可以先打印看一下login这个组件对象,console.log(login),看看login组件引入进来没有,然后login.methods.refreshVerify()调用就表示调用login组件下的refreshVerify函数了
慕容3067478
TA贡献1773条经验 获得超3个赞
将刷新验证码的js抽离,通过import导入,用mixins混入,使用参照:vue中extend,mixins,extends,components,install的几个操作VUE项目开发中使用mixins
添加回答
举报
0/150
提交
取消