vue里的JS要怎么引用.vue里面methods的方法?

在login.vue的methods有一个刷新验证码的方法,在另外一个JS里我想要引用那个刷新验证码的方法,应该怎么写才对?

login.vue文件

<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title"><img src="/static/images/logo.png"></h3>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.username" name="username" type="text" autocomplete="off" placeholder="用户名" @keyup.enter.native="handleLogin" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码" />
        <span ref="showpwd" class="show-pwd" @click="showPwd" @keyup.enter.native="handleLogin">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-form-item prop="verifyCode" class="verify">
        <span class="svg-container">
          <svg-icon icon-class="verify" />
        </span>
        <el-input v-model="loginForm.verifyCode" name="verifyCode" type="text" autocomplete="off" placeholder="验证码" oninput="if(value.length > 4)value = value.slice(0, 4)" @keyup.enter.native="handleLogin" />
      </el-form-item>
      <div class="verify-pos"><img :src="verifyUrl" width="100%" alt="点击刷新验证码" @click="refreshVerify()"></div>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">登 入</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateVerify = (rule, value, callback) => {
      if (value.length !== 4) {
        callback(new Error('请输入正确的验证码'))
      } 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 = '<svg data-v-c8a70580="" data-v-37dfd6fc="" aria-hidden="true" class="svg-icon"><use data-v-c8a70580="" xlink:href="#icon-eyes"></use></svg>'
      } else {
        this.pwdType = 'password'
        this.$refs.showpwd.innerHTML = '<svg data-v-c8a70580="" data-v-37dfd6fc="" aria-hidden="true" class="svg-icon"><use data-v-c8a70580="" xlink:href="#icon-eye"></use></svg>'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#666;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 80%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid #d2d2d2;
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$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: 35px 35px 15px 35px;
    margin: 120px auto;
    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: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 15px auto;
    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-pos img {
    cursor: pointer;
  }
}
</style>

request.js

// response 拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (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()
          })
        })
      }
     /* 我想在这里写入刷新验证码的操作 */
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error)
    Message({
      message: error.error,
      type: 'error',
      duration: 3 * 1000
    })
    return Promise.reject(error)
  }
)
阅读 7.5k
2 个回答

在js里 import login from './login.vue';注意from后面的路径 必要时,可以先打印看一下login这个组件对象,console.log(login),看看login组件引入进来没有, 然后login.methods.refreshVerify()调用就表示调用login组件下的 refreshVerify函数了

推荐问题
宣传栏