【vue 3.0 中使用vue-router详细步骤】

Vue 3.0 中使用 vue-router 的步骤如下:

      • [1. 安装 vue-router:](#1. 安装 vue-router:)
      • [2. 创建一个单独的文件:](#2. 创建一个单独的文件:)
      • [3.main.js 配置路由:](#3.main.js 配置路由:)
      • [4. 在 App.vue 中使用 `<router-view>` 组件:](#4. 在 App.vue 中使用 <router-view> 组件:)
      • [5. 在路由的组件中使用 `<router-link>` 组件进行导航:](#5. 在路由的组件中使用 <router-link> 组件进行导航:)
      • 6.使用push进行跳转

1. 安装 vue-router:

bash 复制代码
npm install vue-router@4.0.0-beta.8

2. 创建一个单独的文件:

javascript 复制代码
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from './views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

3.main.js 配置路由:

javascript 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')

4. 在 App.vue 中使用 <router-view> 组件:

js 复制代码
<template>
  <div>
    <h1>App.vue</h1>
    <router-view />
  </div>
</template>

5. 在路由的组件中使用 <router-link> 组件进行导航:

js 复制代码
<template>
  <div>
    <h2>About.vue</h2>
    <router-link to="/">Go to Home</router-link>
  </div>
</template>

6.使用push进行跳转

js 复制代码
<template>
  <div>
    <button @click="godetalis">跳转</button>
  </div>
</template>
  
  <script>
 import { useRouter } from "vue-router"; //引入路由

export default {
  setup() {
     const $route = useRouter();
    function godetalis() {
      $route.push({name: "你的路由名称"});
    }

    return {
      godetalis
    };
  }
};
</script>
  

以上就是在 Vue 3.0 中使用 vue-router 的详细步骤。

相关推荐
驯狼小羊羔6 分钟前
学习随笔-require和import
前端·学习
excel13 分钟前
🚀 从 GPT-5 流式输出看现代前端的流式请求机制(Koa 实现版)
前端
凸头17 分钟前
Spring Boot接收前端参数的注解总结
前端·spring boot·后端
爱吃甜品的糯米团子37 分钟前
JavaScript 正则表达式:选择、分组与引用深度解析
前端·javascript·正则表达式
excel41 分钟前
Vue SSR 编译器源码深析:ssrTransformShow 的实现原理与设计哲学
前端
excel42 分钟前
深入解析 Vue 3 SSR 编译管线:ssrCodegenTransform 源码全解
前端
excel43 分钟前
深入解析 Vue SSR 编译器的核心函数:compile
前端
IT_陈寒1 小时前
Vue 3性能优化实战:7个关键技巧让我的应用加载速度提升50%
前端·人工智能·后端
excel1 小时前
Vue SSR 错误系统源码解析:createSSRCompilerError 与 SSRErrorCodes 的设计原理
前端
excel1 小时前
Vue SSR 源码解析:ssrTransformModel 深度剖析
前端