当前位置: 首页 > news >正文

路由Vue Router基本用法

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

一、效果

二、实现

1.项目中安装Vue Router插件

pnpm install vue-router@lastest

2.main.js

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

<template>
    <div id="app">
        <h1>hello vite</h1>
    </div>
</template>

    src/views/About.vue

<template>
    <div id="app">
        <h1>hello vite about</h1>
    </div>
</template>

       src/views/news.vue

<template>
    <div id="app">
        <h1>hello vite news</h1>
    </div>
</template>

4.src/router/index.js:

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

<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>

相关文章:

  • QEMU 中 x86_cpu_realizefn 到 ept_emulation_fault 的调用流程解析(macos)
  • 数据库的两种模式
  • 国内首家,百度智能云千帆AppBuilder全面兼容MCP协议
  • dfs(二十一)46. 全排列 中等
  • 【Linux】信号:产生信号
  • 夜莺监控 v8.0 新版通知规则 | 对接飞书告警
  • 【数据分析】数据筛选与访问行列元素3
  • VLLM专题(十九)—兼容 OpenAI 的服务器
  • [极客大挑战 2019]Http_3.19BUUCTF练习day3(1)
  • 聚类算法api初步使用
  • ArcGIS10.X影像智能下载!迁移ArcGIS Pro批量智能高清影像下载工具至ArcGIS!
  • 音频进阶学习二十——DFT离散傅里叶变换
  • html相关常用语法
  • 【docker】--- 详解 WSL2 中的 Ubuntu 和 Docker Desktop 的区别和关系!
  • springboot Actuator 指标分析
  • 在 Kubernetes(k8s)部署过程中常见的问题
  • 数据结构:用C语言实现插入排序
  • WPF 中的 GridSplitter 详解
  • mysql 索引的使用
  • 【数据库备份】docker中数据库备份脚本——MySql备份脚本
  • 马上评|让“贾宝玉是长子长孙”争议回归理性讨论
  • 中华人民共和国和俄罗斯联邦在纪念中国人民抗日战争、苏联伟大卫国战争胜利和联合国成立80周年之际关于进一步深化中俄新时代全面战略协作伙伴关系的联合声明
  • 西南大学教授、重庆健美运动奠基人之一李启圣逝世
  • 陈雯出任外交部离退休干部局局长,此前为外交部办公厅副主任
  • 42岁退役军人高武生命最后时刻:在水中托举近20分钟救出落水孩童
  • 黔西游船倾覆事故84名落水人员已全部找到,10人不幸遇难