vue路由导航简单实现

1.代码

安装路由模块

npm install vue-router

导入路由组件:

import { createRouter, createWebHistory } from 'vue-router'

首先创建三个vue组件显示路由内容:

index.vue

复制代码
<template>
    <!-- 首页跳转 -->
     <router-link to="/my">我的</router-link>&nbsp;
     <router-link to="/goods">商品</router-link><br>
     <router-view></router-view>
     <h1>首页</h1>
</template>
<script setup>

</script>

<style scoped>

</style>

goods.vue

复制代码
<!--vue简单框架-->
<template>
<div class="">
    <router-link to="/">返回首页</router-link><br>
    <h1>商品订单</h1>
</div>
</template>

<script lang='ts' setup name="">
</script>

<style scoped>
</style>

my.vue

复制代码
<!--vue简单框架-->
<template>
<router-link to="/">返回首页</router-link><br>
<h1>个人中心</h1>
</template>

<script lang='ts' setup name="">

</script>

<style scoped>
</style>

App.vue:

复制代码
<template>
      <!-- 路由视图 -->
     <router-view></router-view>
</template>

<script setup>

</script>

<style scoped>

</style>

router文件下的index.js

复制代码
//导入组件
import { createRouter, createWebHistory } from 'vue-router'
import my from '../components/person/my.vue'
import goods from '../components/person/goods.vue'
import index from '../components/person/index.vue'
//配置路由
const routes=[
    {
        path:"/",
        component:index,
    },
    {
    path:"/my",
    component:my,
    },
    {
    path:"/goods",
    component:goods,
    }
];
//创建路由
const router=createRouter({
    routes,
   history:createWebHistory(),

})
//导出路由
export default router

main.js:

复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from '@/router'

createApp(App)
.use(router)
.mount('#app')

2.运行结果

首页

我的

商品

相关推荐
不想上班只想要钱22 分钟前
vue3+vite创建的项目,运行后没有 Network地址
前端·javascript·vue.js
流***陌41 分钟前
手办盲盒抽赏小程序前端功能设计:兼顾收藏需求与抽赏乐趣
前端·小程序
岁月宁静1 小时前
在富文本编辑器中封装实用的 AI 写作助手功能
前端·vue.js·人工智能
金士顿1 小时前
为什么MainWindow.xaml绑定的datacontext,EtherCATSuiteCtrl.xaml直接用了?
前端
533_1 小时前
[css] flex布局中的英文字母不换行问题
前端·css
浮游本尊2 小时前
React 18.x 学习计划 - 第四天:React Hooks深入
前端·学习·react.js
future_studio2 小时前
聊聊 Unity(小白专享、C# 小程序 之 日历、小闹钟)
前端·html
Yeats_Liao2 小时前
Go Web 编程快速入门 · 04 - 请求对象 Request:头、体与查询参数
前端·golang·iphone
祈祷苍天赐我java之术3 小时前
Redis 数据类型与使用场景
java·开发语言·前端·redis·分布式·spring·bootstrap
草莓熊Lotso3 小时前
C++ 方向 Web 自动化测试入门指南:从概念到 Selenium 实战
前端·c++·python·selenium