vue3+axios+router实现页面跳转及登录
本篇文章主要是,使用 vite 创建一个vue3 书籍商城的小型案例,项目中主要运用到路由router及接口axios等知识点。
1.开始搭建项目框架,使用vite来构建项目
npm create vite@latest
2.由于vite构建的项目中需要自己手动下载路由以及创建路由文件夹,所以在创建好的项目文档中找到src文件夹,在src文件夹下创建router文件夹,并且在其下创建index文件,对于index文件中要写的内容如下,在此之前还需要创建一个views文件夹,本次项目主要用到三个页面,所以需要在views文件夹下需创建三个文件,包括HomeView.vue、AboutView.vue、UserLoginView.vue。这些准备好之后还有重要的一点就是手动安装router。
npm install vue-router//终端中安装router
index.js文件中的内容:
import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
const routes = [
{//首页
path: "/",
name: "home",
component: HomeView,
},
{//书籍商城
path: "/about",
name: "about",
component: () =>
import( "../views/AboutView.vue"),
},
{//登录页
path: "/userlogin",
name: "userlogin",
component: () =>
import("../views/UserLoginView.vue"),
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
export default router;
3.上述内容完成之后,还需要在main.js中引入router,不然会报错,以及在app.vue中需要撰写跳转过程。
import router from "./router";
createApp(App).use(router).mount("#app");
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
nav {
padding: 40px;
}
nav a {
font-weight: bold;
font-size: 1.4rem;
color: #2c3e50;
text-decoration: none;
}
nav a.router-link-exact-active {
color: #42b983;
}
4.接下来就是书写每个页面的代码,首先是首页,首页中未涉及过于复杂的过程,只是一个单纯的静态页面。
| {{ item.type }}{{ item.title }} | {{ item.type }}{{ item.title }} |
好书推荐
-
{{item.title}}
{{ item.intro }}
{{ item.price }}
首页效果图:

5.其次是书籍商城页面,本页面中由于查询之后会展示相应的书籍,所以涉及到了接口问题,还需要另外自己手动下载接口,对于接口文档是借用小说搜索 – LRY_API这个网站的。
npm install axios --save//在终端中下载接口
{{ title }}
搜索结果
-
{{item.title}}
作者:{{ item.author }}
类型:{{ item.fictionType }}
简介:{{ item.descs }}
出版时期:{{ item.updateTime }}
书籍商城效果图:

6.最后就是登录界面,登录界面功能要相对复杂一些,在登录界面中点击其他界面是禁止的,同时在登录之后会跳转到书籍商城界面,内容如下:
{{ msg }}
![]()
import {useRouter,onBeforeRouteLeave} from 'vue-router'; import {ref} from "vue"; const router = useRouter(); const msg=ref('欢迎登录'); const username=ref(null); const password=ref(null); const login=()=>{ if(username.value.value === 'zhangsan' && password.value.value==='123456'){ window.localStorage.setItem('userToken',username.value.value+password.value.value) router.push({ path:'/about', query:{ username:"zhangsan", password:123456 } }) }else{ alert('用户名或密码错误!') } console.log(username.value.value) } onBeforeRouteLeave((to)=>{ let userToken=localStorage.getItem('userToken') if(to.name!='userlogin' && userToken==null){ return false; } }) .box{ width: 50rem; height: 30.5rem; background-color: rgba(188, 176, 212, 0.6); margin-left: auto; margin-right: auto; } .title { text-align: center; font-size: 2rem; color: rgba(115, 87, 172, 0.6); // margin-top: 1rem; padding-top: 0.8rem; } .box_form { width: 50rem; height: 18rem; margin-left: auto; margin-right: auto; .img_control{ margin-top: 6.5rem } .pic{ margin-left: 3rem; float: left; } img{ width: 15rem; height: 15rem; } .form-input { width: 20rem; height: 2.5rem; padding-left: 0.5rem; border-radius: 0.5rem; border: none; } .paw{ margin-top: 2rem; margin-bottom: 1rem; margin-left: 0.7rem; } .form-label{ margin-left: 2rem; margin-right: 1rem; a{ // display: block; text-decoration: none; color: rgb(58, 58, 57); &:hover{ color: #f17e0b; } } } .form-button { width: 20.5rem; height: 2.5rem; margin-top: 1rem; margin-left: 6.5rem; font-size: 1.8rem; border: none; border-radius: 0.8rem; font-family: "宋体"; background: rgb(245, 150, 199); color: #fff; font-weight: 400; cursor: pointer; &:hover{ background: #f17e0b; } } }
登录效果图:

7.最后来看看实际效果吧
书籍商城案例
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/5e7a44c3f6.html
