Vue3【二十一】Vue 路由模式(createWebHashHistory /createWebHistory ) ...

打印 上一主题 下一主题

主题 857|帖子 857|积分 2571

Vue3【二十一】Vue 路由模式(createWebHashHistory /createWebHistory )和RouterLink写法

   Vue3【二十一】Vue 路由模式和普通组件目录布局
createWebHistory history模式:url不带#号,需要后端做url适配 适合贩卖项目 利于seo
createWebHashHistory Hash模式:url 带#号,不需要url适配,比较适合后端项目 不利于seo
路由命名 和 配置路由规则
Header 组件是一样平常组件一样平常放到components文件夹中
News About Home 组件是路由组件,一样平常存放在pages或views文件夹中
/*注意
1、路由渐渐通常存放在pages或views文件夹,一样平常组件通常存放在components文件夹中
2、通过点击路由链接,切换路由组件,视觉上‘消失’了的路由组件,默认是被 卸载 掉的,需要的时间再去挂载
3、路由组件切换时,会触发组件的beforeRouteEnter、beforeRouteUpdate、>beforeRouteLeave钩子函数
  实例截图


目录布局


代码

app.vue

  1. <template>
  2.     <div class="app">
  3.         <Header></Header>
  4.         <!-- 导航区 -->
  5.         <div class="navigate">
  6.             <RouterLink to="/home" active-class="active"> 首页 </RouterLink>
  7.             <!-- <RouterLink to="/news" active-class="active"> 新闻 </RouterLink> -->
  8.             <!-- to的对象写法 -->
  9.             <!-- 名称跳转 -->            
  10.             <RouterLink :to="{name:'xinwen'}" active-class="active"> 新闻 </RouterLink>
  11.             <!-- 路径跳转 -->
  12.             <RouterLink :to="{path:'/about'}" active-class="active"> 关于 </RouterLink>
  13.         </div>
  14.         <!-- 展示区 -->
  15.         <div class="main-content">
  16.             <RouterView />
  17.         </div>
  18.     </div>
  19. </template>
  20. <script lang="ts" setup name="App">
  21. // npm install vue-router //安装路由器
  22. import { RouterView } from 'vue-router';
  23. import Header from '@/components/Header.vue';
  24. // Header 组件是一般组件一般放到components文件夹中
  25. // News About Home 组件是路由组件,一般存放在pages或views文件夹中
  26. /*注意
  27. 1、路由逐渐通常存放在pages或views文件夹,一般组件通常存放在components文件夹中
  28. 2、通过点击路由链接,切换路由组件,视觉上‘消失’了的路由组件,默认是被 卸载 掉的,需要的时候再去挂载
  29. 3、路由组件切换时,会触发组件的beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave钩子函数
  30. */
  31. </script>
  32. <style scoped>
  33. .app {
  34.     background-color: #4fffbb;
  35.     box-shadow: 0 0 10px;
  36.     border-radius: 10px;
  37.     padding: 10px;
  38. }
  39. .navigate {
  40.     display: flex;
  41.     justify-content: space-around;
  42.     margin: 0 100px;
  43. }
  44. .navigate a {
  45.     display: block;
  46.     text-align: center;
  47.     width: 90px;
  48.     height: 40px;
  49.     line-height: 40px;
  50.     border-radius: 10px;
  51.     background-color: #818080;
  52.     text-decoration: none;
  53.     color: #fff;
  54.     /* font-size: 5px; */
  55.     letter-spacing: 5px;
  56. }
  57. .navigate a.active {
  58.     color: #ffc268;
  59.     background-color: #fff;
  60.     border: 1px solid #ffc268;
  61.     font-weight: 900;
  62.     /* text-shadow: 0 0 1px black; */
  63.     font-family: 微软雅黑;
  64. }
  65. .main-content {
  66.     margin: 0 auto;
  67.     margin-top: 30px;
  68.     margin-bottom: 30px;
  69.     border-radius: 10px;
  70.     width: 90%;
  71.     height:400px;
  72.     border: 1px solid;
  73. }
  74. </style>
复制代码
Header.vue

  1. <template>
  2.     <h2 class="title">Vue3 路由和组件页面切换测试</h2>
  3. </template>
  4. <script setup lang="ts" name="Header">
  5. </script>
  6. <style scoped>
  7. .title{
  8.     text-align: center;
  9.     word-spacing: 5px;
  10.     margin: 30px 0;
  11.     height: 70px;
  12.     line-height: 70px;
  13.     background-image: linear-gradient(45deg, #cecece, #fff);
  14.     border-radius: 10px;
  15.     box-shadow: 0 0 2px;
  16.     font-size: 30px
  17. }
  18. </style>
复制代码
index.ts

  1. // 创建一个路由器,并暴漏出去
  2. // 第一步:引入createRouter
  3. import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
  4. // 引入各种组件
  5. import Home from '@/pages/Home.vue'
  6. import About from '@/pages/About.vue'
  7. import News from '@/pages/News.vue'
  8. // 第二步:创建路由器
  9. const router = createRouter({
  10.     // 配置路由模式
  11.     // createWebHistory history模式:url不带#号,需要后端做url适配 适合销售项目 利于seo
  12.     // createWebHashHistory Hash模式:url 带#号,不需要url适配,比较适合后端项目 不利于seo
  13.     history: createWebHistory(),
  14.     // 配置路由规则
  15.     routes: [
  16.         // { path: '/', redirect: '/home' },
  17.         // { path: '/home', component: Home },
  18.         // { path: '/about', component: About },
  19.         // { path: '/news', component: News }
  20.         // 路由命名
  21.         { path: '/', redirect: '/home' },
  22.         { path: '/home', name: 'zhuye', component: Home },
  23.         { path: '/about', name: 'guanyu', component: About },
  24.         { path: '/news', name: 'xinwen', component: News }
  25.     ]
  26. })
  27. // 第三步:导出路由器
  28. export default router
复制代码
home.vue

  1. <template>
  2.     <div class="home">
  3.         <img src="/public/logo.png" alt="">
  4.     </div>
  5. </template>
  6. <script setup lang="ts" name="Home">
  7. </script>
  8. <style scoped>
  9. .home {
  10.     display: flex;
  11.     justify-content: center;
  12.     align-items: center;
  13.     height: 100%;
  14. }
  15. img {
  16.     width: 10%;
  17. }
  18. </style>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

惊落一身雪

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表