当前位置: 首页 > article >正文

Vue3 路由基础 01

npm i vue-router

创建src/router/index.ts

index.ts

// 创建一个路由器,并暴露出去

// 第一步:引入vue-router
import { createRouter, createWebHistory } from 'vue-router'

// 引入可能要呈现的组件
import Home from '@/components/Home.vue'
import About from '@/components/About.vue'
import News from '@/components/News.vue'
// 创建路由器
const router = createRouter({
    history: createWebHistory(), // 路由器的工作模式
    routes:[
        {
            path: '/home',
            component:Home
        },
        {
            path: '/news',
            component:News
        },
        {
            path: '/about',
            component:About
        }
    ]
});

// 暴露出去 router
export default router

main.ts

import './assets/main.css'
// 引入createApp,用来创建vue实例
import { createApp } from 'vue'
// 引入根组件
import App from './App.vue'
// 引入路由器
import router from './router'
// 创建vue实例
const app = createApp(App)
// 挂载路由器
app.use(router)
// 挂载实例
app.mount('#app')

App.vue

<template>
  <div id="app">
    <h1>Vue路由测试</h1>

    <!-- 导航区 -->
     <div class="navigate">
      <RouterLink to="/home" active-class="acti-class">首页</RouterLink>
      <RouterLink to="/news" active-class="acti-class">新闻</RouterLink>
      <RouterLink to="/about" active-class="acti-class">关于</RouterLink>
     </div>

     <!-- 展示区 -->
      <div class="show">
        此处以后可能要展示各种组件,到底展示哪个组件,需要看路径
        <RouterView></RouterView>

      </div>
  </div>
</template>

<script lang="ts" setup>
// 引入路由视图组件
import { RouterView , RouterLink} from 'vue-router';
</script>

<style scoped>


body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

#app {
  text-align: center;
  margin-top: 50px;
}

h1 {
  color: #333;
  font-size: 36px;
}

.navigate {
  margin: 20px 0;
}

.navigate a {
  text-decoration: none;
  color: #00ffe1;
  font-size: 18px;
  margin: 0 15px;
}

.navigate a.acti-class {
  color: #0056b3;
}

.show {
  border: 1px solid #ddd;
  padding: 20px;
  margin: 20px;
  background-color: #f9f9f9;
  font-size: 18px;
  color: #555;
  border-radius: 8px;
}

</style>

注意点

  • 路由组件通常存放在pagesviews文件夹,一般组件通常存放在components文件夹。

    一般组件:<Component/>

    路由组件:<RouterView/>

  • 通过点击导航,视觉效果上“消失” 了的路由组件,默认是被卸载掉的,需要的时候再去挂载

router-link和RouterLink区别

  • <router-link> 是模板中的写法,用在 .vue 文件的 <template> 部分,Vue 会自动解析它。
  • RouterLink 是在脚本或 JSX 中的直接引用,用于非模板环境(如组合 API、JSX 或渲染函数)。

它们背后都是指向同一个路由组件,功能是相同的,唯一的区别是使用场景和语法风格不同。

路由器工作模式

  1. history模式

    优点:URL更加美观,不带有#,更接近传统的网站URL

    缺点:后期项目上线,需要服务端配合处理路径问题,否则刷新会有404错误。

    const router = createRouter({
    	history:createWebHistory(), //history模式
    	/******/
    })
    
  2. hash模式

    优点:兼容性更好,因为不需要服务器端处理路径。

    缺点:URL带有#不太美观,且在SEO优化方面相对较差。

    const router = createRouter({
    	history:createWebHashHistory(), //hash模式
    	/******/
    })
    

to的两种写法

<!-- 第一种:to的字符串写法 -->
<router-link active-class="active" to="/home">主页</router-link>

<!-- 第二种:to的对象写法 -->
<router-link active-class="active" :to="{path:'/home'}">Home</router-link>

命名组件

const router = createRouter({
    history: createWebHistory(), // 路由器的工作模式
    routes:[
        {
            name:'jia',
            path: '/home',
            component:Home
        },
        {
            path: '/news',
            component:News
        },
        {
            path: '/about',
            component:About
        }
    ]
});
<router-link active-class="active" :to="{name:'jia'}">Home</router-link>

<router-link active-class="active" :to="{path:'/home'}">Home</router-link>

1


http://www.kler.cn/news/355768.html

相关文章:

  • “大放水救股市”会有怎样的结果?
  • .NET 6 API + Middleware + Audit rail
  • 特征工程在营销组合建模中的应用:基于因果推断的机器学习方法优化渠道效应估计
  • Kettle自定义数据库连接
  • 什么叫CMS?如何使用CMS来制作网站?
  • Linux之实战命令43:dmesg应用实例(七十七)
  • Json-Rpc框架(项目设计 —— 客户端模块功能详细介绍)
  • 400行程序写一个实时操作系统(十):用面向对象思想构建抢占式内核
  • Redis 高可用:从主从到集群的全面解析
  • C++实现本地资源文件编译时加载
  • Leetcode 921 Shortest Path in Binary Matrix
  • Cursor:你的AI编程助手 - 核心功能全解析
  • 特斯拉Robotaxi发布会2024:自动驾驶未来的开端
  • 华为OD机试2024年真题( 最远足迹)
  • OBOO鸥柏丨 21.5 寸自助服务终端机智能科技查询一体新势力
  • python异常检测-局部异常因子(LOF)算法
  • Linux下使用c语言获取一个挂载文件夹可用存储空间以及使用率
  • 【已解决】docx4j 结合Thymeleaf 的各种依赖问题(坑)
  • 【Spring声明式事务失效的12种场景测试】
  • Redis 数据类型Bitmaps(位图)