⑥ 在vue中引入路由

什么是路由:管理页面的跳转(vue-router)

官方文档

复制代码
https://router.vuejs.org/zh/introduction.html

第一步:安装路由后启动项目

复制代码
npm install --save vue-router

配置独立的路由文件

在src目录文件下新建一个文件夹router,新建文件index.js

先要有页面,创建两个vue页面,引入到index.js里面

复制代码
import { createRouter,createWebHashHistory } from "vue-router";
import  homevue  from "../view/Homevue";
import  about  from "../view/About";

//配置信息中需要页面的相关配置

const routes = [
    {
        path: '/',
        component:homevue//对应的那个组件
    },
    {
        path:'/about',
        component:about
    }
]


const router = createRouter({
	// createWebHistory      访问的时候网址是http://localhost:8080/about
    // 此种方式需要后台配合做重定向,否则会出现404问题
    // 原理:HS pushState()


    // createWebHashHistory  访问的时候网址是http://localhost:8080/#/about
    // 原理:a标签的锚点链接
    history:createWebHashHistory(),
    routes
})


//导出
export default router;

引入路由到项目当中

在main.js引入

指定路由显示入口 router-view

然后在app.vue页面上显示

实现路由的跳转

相关推荐
2601_958492553 小时前
Optimizing Engagement with Freehead Skate - HTML5 Game - Construct 3
前端·html·html5
茉莉玫瑰花茶4 小时前
工作流的常见模式 [ 1 ]
java·服务器·前端
zhangxingchao4 小时前
AI应用开发六:企业知识库
前端·人工智能·后端
山峰哥5 小时前
SQL慢查询调优实战:从全表扫描到索引覆盖的完整复盘
前端·数据库·sql·性能优化
红尘散仙5 小时前
一个 `#[uniffi::export]`,把 Rust 接进 React Native
前端·后端·rust
moshuying5 小时前
AI Coding 最大的 token 黑洞,可能根本不是 prompt
前端
红尘散仙5 小时前
一行 `#[specta::specta]`,让 Tauri IPC 有类型
前端·后端·rust
lichenyang4536 小时前
HarmonyOS HMRouter 接入记录:从普通 Tab Demo 到路由跳转
前端
木斯佳6 小时前
前端八股文面经大全:腾讯WXG暑期前端一面(2026-05-15)·面经深度解析
前端·面试·笔试