简体   繁体   English

Vue 登录表单 - 多用户

[英]Vue Login Form - Multiple User

I've created a login form with Vue.js.我用 Vue.js 创建了一个登录表单。 I'm new with the Vue.js framework and want to write a test.我是 Vue.js 框架的新手,想编写一个测试。 How can I get more Users?如何获得更多用户? I can only login with one created user with我只能使用一个创建的用户登录

username: 'tiko', password: 'miko'

el: '#app',
  
  data: {
    username: '',
    password: '',
        username2: '',
    password2: '',
    credentials: {
      username: 'tiko', password: 'miko', 
      
      
    },
     
   
    errors: {}
  },
  
  methods: {
    onSubmit() {
      if( 
        this.credentials.username !== this.username || 
        this.credentials.password !== this.password 
      ) {
        this.errors = new Error()
        this.errors.set('Invalid credentials!')
      }
      else {
        alert('Success!')
      }
    }
  }
  
})

CODEPEN 代码笔

Fist off, this is not how login forms should be made in the real world at all.首先,这根本不是在现实世界中制作登录表单的方式。 You need encryption and you need to externally store the credentials on a database.您需要加密,并且需要将凭据从外部存储在数据库中。

However its fine when you are just using this to play around with VueJS然而,当你只是用它来玩 VueJS 时它很好

Second off, you are currently storing everything as strings in sepperate variables.其次,您目前将所有内容作为字符串存储在单独的变量中。 If you want to be able to store multiple credentials, i would store an object that maps a username to a password如果您希望能够存储多个凭据,我将存储一个将用户名映射到密码的对象

data: {
  credentials: {
    "bob": "password123"
    "hank": "dolfins123"
  }
}

then you can validate if the credentials in the input field are correct using something like:然后您可以使用以下内容验证输入字段中的凭据是否正确:

methods: {
    onSubmit() {
      if(credentials[this.username] !== this.password) {
        this.errors = new Error();
        this.errors.set('Invalid credentials!');
      }
      else {
        alert('Success!');
      }
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM