【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 [email protected]

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

相关推荐
SleepyZone5 分钟前
Cline 源码浅析 - 从输入到输出
前端·ai编程·cline
Struggler2819 分钟前
pinia-基于monorepo的项目结构管理
前端
Struggler28113 分钟前
SSE的使用
前端
用户58061393930020 分钟前
前端文件下载实现深度解析:Blob与ObjectURL的完美协作
前端
Lin866623 分钟前
Vue 3 + TypeScript 组件类型推断失败问题完整解决方案
前端
coding随想23 分钟前
从零开始:前端开发者的SEO优化入门与实战
前端
前端工作日常25 分钟前
我理解的JSBridge
前端
Au_ust26 分钟前
前端模块化
前端
顺丰同城前端技术团队26 分钟前
还不会用 Charles?最后一遍了啊!
前端
BUG收容所所长27 分钟前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法