Vue3.0极速入门 - 登录demo

Talk is cheap, Show the code

·

在完成npm和vue的环境安装,并了解了基本的目录和文件结构以后,直接写一个带登录和首页的demo做示例,快速了解一个vue工程的创建和基本的页面跳转

第一步创建工程

1、选择手动模式创建工程

复制代码
npm create app-demo

2、添加router到工程中

第二步:创建登录页面

1、新建文件

2、文件代码

LoginByCode.vue

复制代码
<template>
    <div class="login-code">
        <input placeholder="请输入手机号"/>
        <br/>
        <input placeholder="请输入手机验证码"/>
    </div>
</template>

<script>
export default {
    name: 'LoginByCode'
}
</script>

<style scoped>
.login-code {
    position:relative;
}
</style>

LoginByPwd.vue

复制代码
<template>
    <div>
        <input placeholder="请输入手机号或账号"/>
        <br/>
        <input placeholder="请输入密码"/>
    </div>
</template>

<script>
export default {
    name: 'LoginByPwd'
}
</script>

LoginView.vue

复制代码
<template>
  <div class="login-containt">
    <img class="logo" src="../assets/logo.png" />
    <login-by-code v-show="logonType === 'code'"></login-by-code>
    <login-by-pwd v-show="logonType === 'pwd'">></login-by-pwd>
    <button class="login-button" v-on:click="onSubmit">登录</button>
    <br />
    <div class="login-bottom-containt">
      <button
        class="change-login-type"
        @click="onChangeLoginType"
        v-show="logonType === 'pwd'"
      >
        验证码登录
      </button>
      <button
        class="change-login-type"
        @click="onChangeLoginType"
        v-show="logonType === 'code'"
      >
        密码登录
      </button>
    </div>
  </div>
</template>

<script>
import LoginByCode from "../components/LoginByCode.vue";
import LoginByPwd from "../components/LoginByPwd.vue";

export default {
  components: { LoginByCode, LoginByPwd },
  name: "LoginView",
  data() {
    return {
      logonType: "pwd",
    };
  },
  methods: {
    onSubmit() {
      this.$router.push('/homePage');
      if (this.$data.logonType === "pwd") {
        // 密码登录
        console.log("密码登录");
      } else {
        // 验证码登录
        console.log("验证码登录");
      }
    },
    onChangeLoginType() {
      if (this.$data.logonType === "pwd") {
        this.$data.logonType = "code";
      } else {
        this.$data.logonType = "pwd";
      }
      console.log("切换登录方式");
    },
  },
};
</script>

<style  scoped>
.login-containt {
  text-align: center;
}

.logo {
  margin-top: 40%;
  width: 100px;
  height: 100px;
}
.login-bottom-containt {
  text-align: center;
}
.login-button {
  margin-top: 40px;
}
.change-login-type {
  text-align: right;
  margin-top: 40px;
}
</style>

3、效果图

第三步:修改路由

修改router/index.js文件

router/index.js

复制代码
import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/homePage',
    name: 'homePage',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  const toPath = to.path;
  const fromPath = from.path;
  console.log(fromPath)
  console.log(toPath)
  next()
});

router.onError((err) => {
  console.log(err)
})


export default router

2、修改App.vue文件

App.vue

复制代码
<template>
  <div id="app" class="app-containt">
    <router-view class="router-containt"></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style>
</style>

3、点击登录按钮后跳转到首页

Vue-Router是如何工作的

1、index.js的route定义是前提

复制代码
const routes = [
    // 通过redirect实现重定向,可以在用户访问默认的url时跳转到指定的登录页面
  {
    path: '/',
    redirect: 'login'
  },
  // 通过component组件方式注册,path是路径,跳转时使用path跳转
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  // 通过chunk方式注册,可以实现延迟加载
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

// 创建route对象
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 通过export default 暴露router对象给外部使用
export default router

2、想要使用必须在main.js挂载

因为使用手动创建模式,vue-cli已经自动将router对象挂在到App对象

复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App).use(router).mount('#app')

3、想要跳转必须在最外层的App.vue定义

router是一个栈结构,router-view相当于路由的rootview,必须预先放在最外层的div里,系统也会默认往router-view注入第一个栈顶vue页面

复制代码
<template>
  <div id="app" class="app-containt">
    <router-view class="router-containt"></router-view>
  </div>
</template>

push、replace和go的使用区别

this.$router.push('/homePage')

往栈中压入homePage页面,浏览器历史增加一条浏览记录

this.$router.replace('/homePage')

用homePage替换栈顶的vue页面,浏览器历史不变

this.$router.go(-1)

推出一个栈顶元素,回到上一个页面

相关推荐
步十人10 小时前
【Vue3】前置知识简单概述(包括ES6核心语法,模块化ESM以及npm基础)
arcgis·npm·vue·es6
有梦想的程序星空1 天前
【环境配置】Vue3项目离线化本地部署echarts全攻略
前端·javascript·vue·echarts
向日的葵0061 天前
vue路由(二)
前端·javascript·vue.js·vue
小妖6662 天前
Hydration completed but contains mismatches
javascript·vue·vuepress
lianyinghhh2 天前
FlowGame 从零上手:开源 AI 工作流编排框架与 Vue 3 接入实战
python·低代码·开源·vue·rag·flowgame·ai工作流编排
爱编程的小金2 天前
告别手写分页逻辑:usePagination 从 50 行到 3 行
javascript·vue·前端分页·alova·usepagination
ok406lhq3 天前
用 MonkeyCode 8 小时搭建自动化内容站:AI Coding 平台实战复盘
ci/cd·vue·ai编程·自动化部署·monkeycode
brycegao3213 天前
Tauri2+Vue3+Ollama 实战|依托 AI 协同开发全离线隐私记账桌面软件(开源)
人工智能·开源·vue·ai编程·tauri·ollama·桌面开发
向日的葵0063 天前
快速了解vue中的路由如何实现(路由一)
前端·vue.js·vue·路由
暗冰ཏོ3 天前
《uni-app 跨端开发完整指南:从基础入门到 H5、小程序、App 发布上线》
前端·小程序·uni-app·vue·html5