【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 的详细步骤。

相关推荐
SadSunset4 分钟前
(19)Bean的循环依赖问题
java·开发语言·前端
JIngJaneIL7 分钟前
基于Java+ vue图书管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
阿蒙Amon13 分钟前
JavaScript学习笔记:10.集合
javascript·笔记·学习
几何心凉15 分钟前
容器平台集群管理和调度
前端
几何心凉20 分钟前
openFuyao 总体定位和解决方案
前端
IT_陈寒29 分钟前
Vue 3.4 实战:5个被低估的Composition API技巧让我的开发效率提升40%
前端·人工智能·后端
VX:Fegn089530 分钟前
计算机毕业设计|基于springboot + vue考勤管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
一 乐34 分钟前
幼儿园管理|基于springboot + vue幼儿园管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
JH灰色35 分钟前
【大模型】-LangChain多模态输入和自定义输出
java·前端·langchain
JIngJaneIL35 分钟前
基于Java + vue校园论坛系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端