Vue3:路由vue-router的使用

一、情景说明

实现Vue项目页面的单页面无抖动跳转
这个时候就需要vue-router插件的支持

Vue2中路由的使用:https://blog.csdn.net/Brave_heart4pzj/article/details/136236978

二、案例

1、安装

安装
npm i vue-router
查看版本
npm list vue-router

2、编写路由器文件

src/router/index.ts

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

// 第一步:引入createRouter
import {createRouter,createWebHistory} from 'vue-router'
// 引入一个一个可能要呈现组件
import Home from '@/components/Home.vue'
import News from '@/components/News.vue'
import About from '@/components/About.vue'

// 第二步:创建路由器
const router = createRouter({
    history:createWebHistory(), //路由器的工作模式(稍后讲解)
    routes:[ //一个一个的路由规则
        {
            path:'/home',
            component:Home
        },
        {
            path:'/news',
            component:News
        },
        {
            path:'/about',
            component:About
        },
    ]
})

// 暴露出去router
export default router

3、配置路由器

main.js

// 引入createApp用于创建应用
import {createApp} from 'vue'
// 引入App根组件
import App from './App.vue'
// 引入路由器
import router from './router'

// 创建一个应用
const app = createApp(App)
// 使用路由器
app.use(router)
// 挂载整个应用到app容器中
app.mount('#app')

4、组件中使用

关键配置:RouterLink、RouterView

<template>
    <div class="app">
        <h2 class="title">Vue路由测试</h2>
        <!-- 导航区 -->
        <div class="navigate">
            <RouterLink to="/home" active-class="active">首页</RouterLink>
            <RouterLink to="/news" active-class="active">新闻</RouterLink>
            <RouterLink to="/about" active-class="active">关于</RouterLink>
        </div>
        <!-- 展示区 -->
        <div class="main-content">
            <RouterView></RouterView>
        </div>
    </div>
</template>

<script lang="ts" setup name="App">
    import {RouterView,RouterLink} from 'vue-router'

</script>

其他方面,和Vue2的使用方法相同。

相关推荐

  1. Vue3vue-router使用

    2024-03-22 06:40:01       20 阅读
  2. vue-router

    2024-03-22 06:40:01       12 阅读
  3. Vue3机制router

    2024-03-22 06:40:01       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 06:40:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 06:40:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 06:40:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 06:40:01       20 阅读

热门阅读

  1. 如何在MySQL 8.0版本中开启远程登录

    2024-03-22 06:40:01       19 阅读
  2. TikCloud天玑云微服务技术选型

    2024-03-22 06:40:01       22 阅读
  3. zoom 在 css中的用法

    2024-03-22 06:40:01       20 阅读
  4. 【Docker】常用命令 docker network ls

    2024-03-22 06:40:01       19 阅读
  5. 我的创作纪念日

    2024-03-22 06:40:01       21 阅读
  6. hive的小文件如何处理

    2024-03-22 06:40:01       20 阅读
  7. ArcGis 地图文档

    2024-03-22 06:40:01       18 阅读
  8. 如何判断对象可以被回收

    2024-03-22 06:40:01       21 阅读