当前位置:   article > 正文

前端学习(1907)vue之电商管理系统电商系统之渲染修改用户的表单的重置操作_const {data:res}=await this.$http.post('login',thi

const {data:res}=await this.$http.post('login',this.loginform);

目录结构

router.js

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Login from './components/Login.vue'
  4. import Home from './components/Home.vue'
  5. import Welcome from './components/Welcome.vue'
  6. Vue.use(Router)
  7. const router = new Router({
  8. routes: [{
  9. path: '/',
  10. redirect: '/login'
  11. },
  12. {
  13. path: '/login',
  14. component: Login
  15. },
  16. {
  17. path: '/home',
  18. component: Home,
  19. redirect:'/welcome',
  20. children: [{
  21. path: '/welcome',
  22. component: Welcome
  23. }]
  24. }
  25. ]
  26. });
  27. //挂载路由导航守卫
  28. router.beforeEach((to, from, next) => {
  29. if (to.path === '/login') return next();
  30. //获取token
  31. const tokenStr = window.sessionStorage.getItem('token')
  32. if (!tokenStr) return next('/login')
  33. next();
  34. })
  35. export default router

login.vue

  1. <template>
  2. <div class="login_container">
  3. <div class="login_box">
  4. <div class="avatar_box">
  5. <img src="../assets/logo.png">
  6. </div>
  7. <!-- 表单区域-->
  8. <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
  9. <!-- 登录区域-->
  10. <el-form-item prop="username">
  11. <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
  12. </el-form-item>
  13. <el-form-item prop="password">
  14. <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
  15. </el-form-item>
  16. <el-form-item class="btns">
  17. <el-button type="primary" @click="login">登录</el-button>
  18. <el-button type="info" @click="resetLoginForm">重置</el-button>
  19. </el-form-item>
  20. </el-form>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default{
  26. data(){
  27. return{
  28. //这是登录表单的数据
  29. loginForm:{
  30. username:'geyao',
  31. password:'12345678'
  32. },
  33. // 表单验证
  34. loginFormRules: {
  35. username: [
  36. { required: true, message: '请输入用户名', trigger: 'blur' },
  37. { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  38. ],
  39. password: [
  40. { required: true, message: '请输入用户密码', trigger: 'blur' },
  41. { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  42. ]
  43. }
  44. }
  45. },
  46. methods:{
  47. resetLoginForm(){
  48. // console.log(this)
  49. this.$refs.loginFormRef.resetFields();
  50. },
  51. login(){
  52. this.$refs.loginFormRef.validate(async valid =>{
  53. if(!valid) return;
  54. const {data:res}=await this.$http.post('login',this.loginForm);
  55. if(res.meta.status!==200) return this.$message.error('登录失败');
  56. this.$message.success('登录成功');
  57. // 1、将登陆成功之后的token, 保存到客户端的sessionStorage中;
  58. // 1.1 项目中出现了登录之外的其他API接口,必须在登陆之后才能访问
  59. // 1.2 token 只应在当前网站打开期间生效,所以将token保存在
  60. window.sessionStorage.setItem('token', res.data.token)
  61. // 2、通过编程式导航跳转到后台主页, 路由地址为:/home
  62. this.$router.push('/home')
  63. });
  64. }
  65. }
  66. }
  67. </script>
  68. <style lang="less" scoped>
  69. .login_container {
  70. background-color: #2b4b6b;
  71. height: 100%;
  72. }
  73. .lo
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号