vue 2.0 中 函数`return false` 使用

vue 2.0 中 函数return false 使用

问题

在注册页面下,判断输入框是否为空,想把这种函数封装一下,外部调用,返回return false 时,只是终止当前的判断函数,不是终止整个注册函数。

  • 判断函数
//判断输入框函数
judgmentInput(formName){
    if (formName.username==="") {
        this.$message.warning("用户名为空");
        return false;
    }
    if (formName.password==="") {
        this.$message.warning("密码为空为空");
        return false;
    }
    // if (formName.newPassword) this.$message.warning("确认密码为空");return false;}
    if (formName.code==="") {
        this.$message.warning("验证码为空");
        return false;
    }
    return true;
},
  • 注册函数
//注册函数
registered(formName){
    this.judgmentInput(formName);
    let data ={
        "username": formName.username,
        "password": formName.password,
    };
    registeredUser(data).then((response)=>{
        console.log(response)
    }).catch();
},

在这种情况下,注册函数也能执行成功?

解决

因为return false 只能结束当前的函数,不能结束调用它的函数。但是你可以用一个变量接收判断函数的返回值,并进行判断是否结束该函数。

  • 修改后端注册函数
//注册函数
registered(formName){
    if (!this.judgmentInput(formName)){
        return false;
    }
    let data ={
        "username": formName.username,
        "password": formName.password,
    };
    registeredUser(data).then((response)=>{
        console.log(response)
    }).catch();
},

这样的话就可以进行判断是否继续执行。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue2.0 使用 Vuex 实现登录,可以将登录状态和用户信息存储在 Vuex 的 store ,以便在应用的任何地方都可以访问和使用。 下面是一个简单的示例代码: 1. 创建 store.js 文件,定义 store 的 state、mutations 和 actions: ```javascript import Vue from 'vue' import Vuex from 'vuex' import axios from 'axios' Vue.use(Vuex) export default new Vuex.Store({ state: { isLoggedIn: false, user: {} }, mutations: { login (state, user) { state.isLoggedIn = true state.user = user }, logout (state) { state.isLoggedIn = false state.user = {} } }, actions: { login ({ commit }, { username, password }) { return axios.post('/api/login', { username, password }) .then(response => { const user = response.data.user localStorage.setItem('token', response.data.token) commit('login', user) return user }) }, logout ({ commit }) { localStorage.removeItem('token') commit('logout') } } }) ``` 2. 在登录页面使用 Vuex 的 mapActions 辅助函数将 login 方法映射到组件的 methods : ```html <template> <div> <input type="text" v-model="username" placeholder="请输入用户名"/> <input type="password" v-model="password" placeholder="请输入密码"/> <button @click="handleLogin">登录</button> </div> </template> <script> import { mapActions } from 'vuex' export default { data () { return { username: '', password: '' } }, methods: { ...mapActions(['login']), handleLogin () { this.login({ username: this.username, password: this.password }) .then(user => { // 登录成功,跳转到首页或其他页面 this.$router.push('/') }) .catch(error => { // 登录失败,提示错误信息 alert(error.response.data.message) }) } } } </script> ``` 3. 在应用的任何地方,都可以通过 Vuex 的 mapState 辅助函数或 $store.state 来访问登录状态和用户信息: ```html <template> <div> <p v-if="isLoggedIn">欢迎,{{ user.name }}!</p> <button v-if="isLoggedIn" @click="handleLogout">退出登录</button> </div> </template> <script> import { mapState, mapActions } from 'vuex' export default { computed: { ...mapState(['isLoggedIn', 'user']) }, methods: { ...mapActions(['logout']), handleLogout () { this.logout() .then(() => { // 退出登录成功,跳转到登录页面 this.$router.push('/login') }) } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值