路由Vue Router基本用法

路由的作用是根据URL来匹配对应的组件,并且无刷新切换模板的内容。vue.js中,可使用Vue Router来管理路由,让构建单页应用更加简单。

一、效果

二、实现

1.项目中安装Vue Router插件

pnpm install vue-router@lastest

2.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')

3. scr/views/Home.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite</h1>
    </div>
</template>

src/views/About.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite about</h1>
    </div>
</template>

src/views/news.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite news</h1>
    </div>
</template>

4.src/router/index.js:

javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router'

import Home from '../views/Home.vue'
import News from '../views/News.vue'
import About from '../views/About.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: '首页',
      redirect: '/home'
    },
    {
      path: '/home',
      name: '首页',
      component: Home
    },
    {
      path: '/news',
      name: '新闻',
      component: News
    },
    {
      path: '/about',
      name: 'Contact',
      component: About
    }
  ]
})

export default router

​

5.scr/app.vue

javascript 复制代码
<template>
  <h1 class="title">Vue路由测试</h1>
  <div class="container">
    <RouterLink to="/home"  class="item"  >首页</RouterLink>
    <RouterLink to="/home"  class="item">产品</RouterLink>
    <RouterLink :to="{ path: '/news' }" class="item" >新闻</RouterLink>
    <RouterLink :to="{ name: 'Contact' }" class="item" >联系我们</RouterLink>
  </div>
  <div class="routerbox">
    <RouterView></RouterView>
  </div>
</template>

<script  setup >
import { RouterLink, RouterView } from 'vue-router'
import { ref, reactive, watch, onMounted } from 'vue';
</script>

 <style  scoped>
h1 {
  text-align: center;
}
.container {
  margin: 0 auto;
  width: 70%;
  display: flex;
  justify-content: space-evenly; 
} 
  .container>.item {
    flex: 0 0 auto;
    background-color: #df6565;
    color: #fff;
    text-decoration: none;
    width: 100px;
    height: 50px;
    border-radius: 10%;
    text-align: center;
    line-height: 50px;
    font-size: 20px;
  }

  .container>.item:hover {
    background-color: #bec9c2;
    color: #ECC980;
  } 
.routerbox {
  margin: 0 auto;
  margin-top: 20px;
  width: 60%;
  height: 200px;
  border: 1px solid #000;
  border-radius: 10px;
  padding: 20px;
} 
</style>
相关推荐
gAlAxy...17 小时前
IntelliJ IDEA 四种项目构建:从普通 Java 到 Maven Web 项目
前端·firefox
my一阁17 小时前
2025-web集群-问题总结
前端·arm开发·数据库·nginx·负载均衡·web
会飞的小妖17 小时前
个人博客系统(十一、前端-简短的配置)
前端
念念不忘 必有回响18 小时前
nginx前端部署与Vite环境变量配置指南
前端·nginx·vite
JIngJaneIL19 小时前
篮球论坛|基于SprinBoot+vue的篮球论坛系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·篮球论坛系统
程序猿阿伟20 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
麦麦大数据21 小时前
D030知识图谱科研文献论文推荐系统vue+django+Neo4j的知识图谱|论文本文相似度推荐|协同过滤
vue.js·爬虫·django·知识图谱·科研·论文文献·相似度推荐
尘觉21 小时前
面试-浅复制和深复制?怎样实现深复制详细解答
javascript·面试·职场和发展
fruge1 天前
Vue Pinia 状态管理实战指南
前端·vue.js·ubuntu
绝无仅有1 天前
某教育大厂面试题解析:MySQL索引、Redis缓存、Dubbo负载均衡等
vue.js·后端·面试