实现Vue的登录页面

迷南。 2023-10-04 22:43 134阅读 0赞

实现Vue的登录页面步骤:

  1. 1.前期准备
  2. 1.1 安装Node.js:从官网下载地址(https://nodejs.org/zh-cn/)安装完成后,在终端输入 node -v 来查询版本号。
  3. 1.2 安装Webpack:在终端输入npm install webpack -g来进行全局安装。
  4. 1.3 安装Vue-cli:在终端输入npm install --global vue-cli来进行全局安装。
  5. 2. 搭建Vue项目
  6. 2.1 创建项目:在终端输入vue init webpack projectname来创建项目。
  7. 2.2 项目目录:进入项目目录中,可以看到srcbuild等文件夹和文件。其中src为我们开发时需要操作的目录,build为我们项目打包的相关配置。
  8. 2.3 导入Element UI:在终端中输入npm i element-ui -S来进行Element UI的安装,并在main.js中导入相关模块。
  9. 3. 实现登录页面
  10. 3.1 修改App.vue:在template中加入router-view组件,用于展示Login.vue等其他组件。
  11. 3.2 创建Login.vue:在components文件夹中新建Login.vue组件,并在其template中构建表单元素。
  12. 3.3 配置路由:在router文件夹中的index.js文件中配置路由。
  13. 4. 实现登录功能
  14. 4.1 导入axios:在终端中输入npm i axios -S来进行axios的安装。
  15. 4.2 导入qsMock:在终端中输入npm i qs mockjs -S来进行qsMock的安装,并在main.js中导入相关模块。
  16. 4.3 编写提交js:在Login.vue中编写submit方法,用于提交表单数据至后端。
  17. 4.4 编写Mock测试数据:在mock文件夹中的index.js文件中编写数据,用于测试前端与后端交互是否正常。
  18. 以上是基本的步骤,具体实现方式可根据具体项目情况进行调整和修改。

以下是一个简单的Vue登录页面的代码:

App.vue

  1. <template>
  2. <div id="app">
  3. <!-- 路由视图 -->
  4. <router-view></router-view>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'App'
  10. }
  11. </script>

Login.vue

  1. <template>
  2. <div class="login-container">
  3. <h2>用户登录</h2>
  4. <form>
  5. <label>用户名:</label>
  6. <input type="text" v-model="username">
  7. <br>
  8. <label>密 码:</label>
  9. <input type="password" v-model="password">
  10. <br>
  11. <button type="submit" @click.prevent="handleSubmit">登录</button>
  12. </form>
  13. </div>
  14. </template>
  15. <script>
  16. import axios from 'axios'
  17. import qs from 'qs'
  18. export default {
  19. name: 'Login',
  20. data () {
  21. return {
  22. username: '',
  23. password: ''
  24. }
  25. },
  26. methods: {
  27. handleSubmit () {
  28. // 提交表单数据至后端
  29. axios.post('login', qs.stringify({
  30. username: this.username,
  31. password: this.password
  32. })).then(res => {
  33. console.log(res)
  34. // 登录成功,跳转到首页
  35. this.$router.push({
  36. path: '/' })
  37. }).catch(err => {
  38. console.log(err)
  39. // 登录失败,给出错误提示
  40. alert('登录失败:' + err.response.data.message)
  41. })
  42. }
  43. }
  44. }
  45. </script>
  46. <style>
  47. .login-container {
  48. margin: 100px auto;
  49. width: 400px;
  50. text-align: center;
  51. }
  52. h2 {
  53. margin-bottom: 20px;
  54. }
  55. form {
  56. text-align: left;
  57. }
  58. label {
  59. display: inline-block;
  60. width: 80px;
  61. margin-right: 10px;
  62. text-align: right;
  63. }
  64. input {
  65. width: 240px;
  66. height: 30px;
  67. padding: 0 10px;
  68. border: 1px solid #ccc;
  69. border-radius: 4px;
  70. outline: none;
  71. margin-bottom: 10px;
  72. }
  73. button {
  74. width: 260px;
  75. height: 40px;
  76. background-color: #409EFF;
  77. color: #fff;
  78. border: none;
  79. border-radius: 4px;
  80. outline: none;
  81. cursor: pointer;
  82. }
  83. button:hover {
  84. background-color: #66B1FF;
  85. }
  86. </style>

router/index.js

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Login from '@/components/Login'
  4. Vue.use(Router)
  5. export default new Router({
  6. routes: [
  7. {
  8. path: '/',
  9. name: 'HelloWorld',
  10. component: HelloWorld
  11. },
  12. {
  13. path: '/login',
  14. name: 'Login',
  15. component: Login
  16. }
  17. ]
  18. })

注意,以上只是一个简单示例代码,真实的项目需要根据具体情况进行相应的调整和修改。另外,为了安全起见,请勿在前端将明文密码传输到后端,应该使用加密方式传输。

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue实现登录后跳转到之前页面

    在项目开发中客户突然提出这样的一个需求,客户需要在查看某一个页面时,这个页面需要登录才能查看评论或者提交问题,或者…等,需要有权限的才能查看的信息,这时客户会点击登录界面,但是