vue3+axios+router实现页面跳转及登录

打印 上一主题 下一主题

主题 1046|帖子 1046|积分 3138

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本篇文章主要是,使用 vite 创建一个vue3 册本商城的小型案例,项目中主要运用到路由router及接口axios等知识点。
1.开始搭建项目框架,使用vite来构建项目
  1. npm create vite@latest
复制代码
2.由于vite构建的项目中需要自己手动下载路由以及创建路由文件夹,所以在创建好的项目文档中找到src文件夹,在src文件夹下创建router文件夹,并且在其下创建index文件,对于index文件中要写的内容如下,在此之前还需要创建一个views文件夹,本次项目主要用到三个页面,所以需要在views文件夹下需创建三个文件,包括HomeView.vue、AboutView.vue、UserLoginView.vue。这些预备好之后尚有重要的一点就是手动安装router。
  1. npm install vue-router//终端中安装router
复制代码
index.js文件中的内容:
  1. import { createRouter, createWebHashHistory } from "vue-router";
  2. import HomeView from "../views/HomeView.vue";
  3. const routes = [
  4.   {//首页
  5.     path: "/",
  6.     name: "home",
  7.     component: HomeView,
  8.   },
  9.   {//书籍商城
  10.     path: "/about",
  11.     name: "about",
  12.     component: () =>
  13.       import( "../views/AboutView.vue"),
  14.   },
  15.   {//登录页
  16.     path: "/userlogin",
  17.     name: "userlogin",
  18.     component: () =>
  19.       import("../views/UserLoginView.vue"),
  20.   },
  21. ];
  22. const router = createRouter({
  23.   history: createWebHashHistory(),
  24.   routes,
  25. });
  26. export default router;
复制代码
3.上述内容完成之后,还需要在main.js中引入router,否则会报错,以及在app.vue中需要撰写跳转过程。
  1. import router from "./router";
  2. createApp(App).use(router).mount("#app");
复制代码
  1. <template>
  2.   <nav>
  3.     <router-link to="/">首页</router-link> |
  4.     <router-link to="/about">书籍商城</router-link> |
  5.     <router-link to="/userlogin">登录</router-link>
  6.   </nav>
  7.   <router-view />
  8. </template>
  9. <style>
  10. #app {
  11.   font-family: Avenir, Helvetica, Arial, sans-serif;
  12.   -webkit-font-smoothing: antialiased;
  13.   -moz-osx-font-smoothing: grayscale;
  14.   text-align: center;
  15.   color: #2c3e50;
  16. }
  17. nav {
  18.   padding: 40px;
  19. }
  20. nav a {
  21.   font-weight: bold;
  22.   font-size: 1.4rem;
  23.   color: #2c3e50;
  24.   text-decoration: none;
  25. }
  26. nav a.router-link-exact-active {
  27.   color: #42b983;
  28. }
  29. </style>
复制代码
4.接下来就是誊写每个页面的代码,起首是首页,首页中未涉及过于复杂的过程,只是一个单纯的静态页面。
  1. <template>
  2.   <div class="home">
  3.     <div class="box"></div>
  4.     <!-- 书籍新闻列表部分 -->
  5.     <div class="booklist">
  6.       <table>
  7.         <tr v-for="item in list">
  8.             <td>{{ item.type }}&nbsp;&nbsp;&nbsp;&nbsp;<a href="">{{ item.title }}</a></td>
  9.             <td>{{ item.type }}&nbsp;&nbsp;&nbsp;&nbsp;<a href="">{{ item.title }}</a></td>
  10.         </tr>
  11.       </table>         
  12.     </div>
  13.     <!-- 好书推荐部分 -->
  14.     <div class="bookrec">
  15.       <div class="booktitle">
  16.         <h3 >好书推荐</h3>
  17.       </div>
  18.       <ul class="recomd">
  19.         <li class="booItem" v-for="item in bookitem">
  20.           <img :src="item.url" alt="">
  21.           <a href="#">{{item.title}}</a>
  22.           <p class="intro">{{ item.intro }}</p>
  23.           <p class="price">{{ item.price }}</p>
  24.         </li>
  25.       </ul>
  26.     </div>
  27.   </div>
  28. </template>
  29. <script setup>
  30. import { ref} from 'vue'
  31. const list=ref([
  32.   {
  33.     type:'[悬疑]',
  34.     title:'河尸诡棺:大河生凶煞,送棺三千里'
  35.   },
  36.   {
  37.     type:'[仙侠]',
  38.     title:'成就系统:我在武道世界里偷偷修仙'
  39.   },
  40.   {
  41.     type:'[历史]',
  42.     title:'一品权臣:权臣护国保民,安定社稷'
  43.   },
  44.   {
  45.     type:'[玄幻]',
  46.     title:'诡道修仙:雨淋血染草,月照鬼守尸'
  47.   },
  48.   {
  49.     type:'[都市]',
  50.     title:'我的空间能修仙:我混的风生水起!'
  51.   },
  52. ])
  53. const bookitem=ref([
  54.   {
  55.     url:'../src/assets/img1.jpeg',
  56.     title:'大明:寒门辅臣',
  57.     intro:'寒门出身,临渊而行,三朝辅臣,巅峰大明!',
  58.     price:'¥23'
  59.   },
  60.   {
  61.     url:'../src/assets/img2.jpeg',
  62.     title:'津门诡事录',
  63.     intro:'浮尸丹鼎、黄仙寻仇、水鬼拉脚、凶灵砌墙……怪力乱神?装神弄鬼!',
  64.     price:'¥26'
  65.   },
  66.   {
  67.     url:'../src/assets/img3.jpeg',
  68.     title:'一品',
  69.     intro:'世家少年入江湖,一刀将这天下捅了个通透。',
  70.     price:'¥28'
  71.   },
  72.   {
  73.     url:'../src/assets/img4.jpeg',
  74.     title:'怒火狂飙',
  75.     intro:'一段视频引发战神狂怒,铁拳霸绝横推一切不平,九万里山河染血。',
  76.     price:'¥35'
  77.   }
  78. ])
  79. </script>
  80. <style lang="scss" scoped>
  81. a{
  82.   text-decoration: none;
  83. }
  84. ul,li{
  85.   list-style: none;
  86. }
  87. .box{
  88.   width: 55rem;
  89.   height: 26rem;
  90.   background: url('../assets/pic3.jpg') no-repeat;
  91.   background-size: 100% 100%;
  92.   margin-left: auto;
  93.   margin-right: auto;
  94. }
  95. .booklist{
  96.   width: 50rem;
  97.   // height: 10rem;
  98.   // background: #fc0;
  99.   margin-left: auto;
  100.   margin-right: auto;
  101.   margin-top: 0.5rem;
  102.   table{
  103.     display: flex;
  104.     flex-wrap: wrap;
  105.   }
  106.   td{
  107.     width: 25rem;
  108.     height: 3rem;
  109.     color: rgb(160, 160, 159);
  110.     background: rgb(188, 205, 251);
  111.     a{
  112.       text-decoration: none;
  113.       color: rgb(46, 45, 44);
  114.       cursor: pointer;
  115.       transition: all 0.5s;
  116.       &:hover{
  117.         color: rgb(233, 115, 37);
  118.       }
  119.     }
  120.   }
  121. }
  122. .bookrec{
  123.   width: 55rem;
  124.   height: 25rem;
  125.   margin-top: 0.5rem;
  126.   background: rgba(219, 206, 245, 0.6);
  127.   margin-left: auto;
  128.   margin-right: auto;
  129.   .booktitle{
  130.     width: 55rem;
  131.     height: 3.2rem;
  132.     border-bottom: 1px rgb(161, 161, 159) solid;
  133.     h3{
  134.       float: left;
  135.       padding-left: 1rem;
  136.     }
  137.   }
  138.   .recomd{
  139.     display: flex;
  140.     justify-content: space-around;
  141.     li{
  142.       width: 10rem;
  143.       img{
  144.         width: 9.6rem;
  145.         height: 12.6rem;
  146.       }
  147.       a{
  148.         color: rgb(60, 60, 58);
  149.         &:hover{
  150.           color: rgb(233, 115, 37);
  151.         }
  152.       }
  153.       .intro{
  154.         -webkit-box-orient: vertical;
  155.         -webkit-line-clamp: 2;
  156.         white-space: nowrap;
  157.         overflow: hidden;
  158.         color: rgb(146, 146, 143);
  159.       }
  160.       .price{
  161.         color: rgb(243, 149, 8);
  162.         font-weight: 400;
  163.       }
  164.     }
  165.   }
  166. }
  167. </style>
复制代码
 首页效果图:

 
5.其次是册本商城页面,本页面中由于查询之后会展示相应的册本,所以涉及到了接口问题,还需要另外自己手动下载接口,对于接口文档是借用小说搜索 - LRY_API这个网站的。
  1. npm install axios --save//在终端中下载接口
复制代码
  1. <template>
  2.   <div class="about">
  3.     <h2>{{ title }}</h2>
  4.     <!-- 搜索 -->
  5.     <div class="search">
  6.         <input type="text" placeholder="请输入要查询的书籍类型" class="searchinput"  @keyup.enter="search" v-model="book">
  7.         <button class="btn" @click="search">搜索</button>
  8.     </div>
  9.     <!-- 搜索结果 -->
  10.     <div class="searchResult">
  11.       <h3>搜索结果</h3>
  12.     </div>
  13.     <ul class="allbook">
  14.       <li class="bdesign" v-for="item in bookList">
  15.         <img :src="item.cover" alt="">
  16.         <div class="intro">
  17.           <h4 >{{item.title}}</h4>
  18.           <p class="bookintro">作者:{{ item.author }}</p>
  19.           <p class="bookintro">类型:{{ item.fictionType }}</p>
  20.           <p class="bookintro">简介:{{ item.descs }}</p>
  21.           <p class="bookintro">出版时期:{{ item.updateTime }}</p>
  22.         </div>
  23.       </li>
  24.     </ul>
  25.   </div>
  26. </template>
  27. <script setup>
  28. // import { reactive } from 'vue';
  29. import {useRoute} from 'vue-router'
  30. import {ref} from 'vue'
  31. import axios from 'axios'
  32. const route=useRoute();
  33. // let data=reactive({
  34. //     query: route.query
  35. // })
  36. const title=ref('欢迎来到阅读站')
  37. const bookList = ref('')
  38. const book=ref('')
  39. //调用书籍目录接口
  40. const search = () => {
  41.   axios.get(`https://api.pingcc.cn/fiction/search/title/小说/1/10`)
  42.    
  43.   .then((result) => {
  44.     bookList.value=result.data.data
  45.     console.log(result.data);
  46.         })
  47.   .catch((err)=>{
  48.     alert("书籍输入错误")
  49.     console.log(err)
  50.   })
  51. }
  52. </script>
  53. <style lang="scss" scoped>
  54. a{
  55.         text-decoration: none;
  56. }
  57. ul,li,ol{
  58.         list-style: none;
  59. }
  60. .about{
  61.   width:55rem;
  62.   height:100rem;
  63.   background:rgba(214, 197, 248, 0.6);
  64.   margin-left: auto;
  65.   margin-right: auto;
  66.   h2{
  67.     color: rgb(39, 14, 162);
  68.   }
  69. }
  70. .searchinput{
  71.     width: 30rem;
  72.     height: 2.3rem;
  73.     margin-right: 1rem;
  74. }
  75. .btn{
  76.     width: 5rem;
  77.     height: 2.3rem;
  78.     background: rgb(66, 104, 240);
  79.     color: #fff;
  80.     border: none;
  81.     border-radius: 0.5rem;
  82.     cursor: pointer;
  83.     transition: all 0.5s;
  84. }
  85. .btn:hover{
  86.   background: rgb(245, 185, 66);
  87. }
  88. .searchResult{
  89.   margin-top: 1.5rem;
  90.   width: 55rem;
  91.   height: 3.2rem;
  92.   // background: #59c71e;
  93.   border-bottom: 1px #b1aeae solid;
  94.   h3{
  95.     padding-left: 1rem;
  96.     float: left;
  97.   }
  98. }
  99. .allbook{
  100.   margin-top: 1rem;
  101.   display: flex;
  102.   justify-content: space-between;
  103.   flex-wrap: wrap;
  104.   align-content: flex-start;
  105.   padding:0  10px;
  106.   .bdesign{
  107.     display: flex;
  108.     padding-top: 2rem;
  109.    
  110.     img{
  111.       width: 12rem;
  112.       height: 14rem;
  113.     }
  114.     .intro{
  115.       width: 12rem;
  116.       height: 14rem;
  117.       // background: #c64848;
  118.       margin-left: 0.5rem;
  119.       .bookintro{
  120.         -webkit-box-orient: vertical;
  121.         -webkit-line-clamp: 2;
  122.         white-space: nowrap;
  123.         // text-overflow: ellipsis;
  124.         overflow: hidden;
  125.         text-align: justify;
  126.       }
  127.     }
  128.   }
  129.   
  130. }
  131. </style>
复制代码
 册本商城效果图:

 
6.最后就是登录界面,登录界面功能要相对复杂一些,在登录界面中点击其他界面是禁止的,同时在登录之后会跳转到册本商城界面,内容如下:
  1. <template>
  2.         <div class="box">
  3.                 <div class="title">
  4.                    <h3>{{ msg }}</h3>
  5.             </div>
  6.                 <div class="box_form">
  7.                         <form method="post">
  8.                                 <div class="img_control">
  9.                                     <img src="../assets/pic.jpeg" class="pic"/>
  10.                             </div>
  11.                             <label class="form-label txt" for="username">用&nbsp;户&nbsp;名:</label>
  12.                             <input  class="form-input txt" type="text" placeholder="用户名" required="required" ref="username"/><br>
  13.                             <label class="form-label" for="password">密&nbsp;&nbsp;码:</label>
  14.                             <input  class="form-input paw" type="password" placeholder="密码" required="required" ref="password"/><br>
  15.                             <label  class="form-label" for="password">没有账号?<a href="#">点击注册</a></label><br>
  16.                             <input  class="form-button" type="submit" value="登录" @click="login"/>
  17.                         </form>
  18.                 </div>
  19.         </div>
  20.    
  21. </template>
  22. <script setup>
  23. import {useRouter,onBeforeRouteLeave} from 'vue-router';
  24. import {ref} from "vue";
  25. const router = useRouter();
  26. const msg=ref('欢迎登录');
  27. const username=ref(null);
  28. const password=ref(null);
  29. const login=()=>{
  30.         if(username.value.value === 'zhangsan' && password.value.value==='123456'){
  31.                 window.localStorage.setItem('userToken',username.value.value+password.value.value)
  32.                 router.push({
  33.                     path:'/about',
  34.                 query:{
  35.                         username:"zhangsan",
  36.                         password:123456
  37.                 }
  38.             })
  39.         }else{
  40.                 alert('用户名或密码错误!')
  41.         }
  42.         console.log(username.value.value)
  43. }
  44. onBeforeRouteLeave((to)=>{
  45.     let userToken=localStorage.getItem('userToken')
  46.     if(to.name!='userlogin' && userToken==null){
  47.         return false;
  48.     }
  49. })
  50. </script>
  51. <style lang="scss" scoped>
  52. .box{
  53.         width: 50rem;
  54.         height: 30.5rem;
  55.         background-color: rgba(188, 176, 212, 0.6);
  56.         margin-left: auto;
  57.         margin-right: auto;
  58. }
  59. .title {
  60.         text-align: center;
  61.         font-size: 2rem;
  62.         color: rgba(115, 87, 172, 0.6);
  63.         // margin-top: 1rem;
  64.         padding-top: 0.8rem;
  65. }
  66. .box_form {
  67.         width: 50rem;
  68.         height: 18rem;
  69.         margin-left: auto;
  70.         margin-right: auto;
  71.     .img_control{
  72.         margin-top: 6.5rem
  73.     }
  74.     .pic{
  75.         margin-left: 3rem;
  76.         float: left;
  77.     }
  78.     img{
  79.         width: 15rem;
  80.         height: 15rem;
  81.     }
  82.     .form-input {
  83.         width: 20rem;
  84.             height: 2.5rem;
  85.             padding-left: 0.5rem;
  86.             border-radius: 0.5rem;
  87.                 border: none;
  88.     }
  89.     .paw{
  90.         margin-top: 2rem;
  91.             margin-bottom: 1rem;
  92.                 margin-left: 0.7rem;
  93.     }
  94.     .form-label{
  95.         margin-left: 2rem;
  96.                 margin-right: 1rem;
  97.                 a{
  98.                         // display: block;
  99.                         text-decoration: none;
  100.                         color: rgb(58, 58, 57);
  101.                         &:hover{
  102.                                 color: #f17e0b;
  103.                         }
  104.                 }
  105.     }
  106.     .form-button {
  107.         width: 20.5rem;
  108.             height: 2.5rem;
  109.             margin-top: 1rem;
  110.             margin-left: 6.5rem;
  111.             font-size: 1.8rem;
  112.                 border: none;
  113.                 border-radius: 0.8rem;
  114.             font-family: "宋体";
  115.                 background: rgb(245, 150, 199);
  116.                 color: #fff;
  117.                 font-weight: 400;
  118.                 cursor: pointer;
  119.                 &:hover{
  120.                         background: #f17e0b;
  121.                 }
  122.     }
  123. }
  124. </style>
复制代码
 登录效果图:

 
7.最后来看看现实效果吧
     册本商城案例
  

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表