路由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>
相关推荐
爱分享的鱼鱼16 分钟前
对比理解 Vue 响应式 API:data(), ref、reactive、computed 与 watch 详解
前端·vue.js
JS_GGbond17 分钟前
【性能优化】给Vue应用“瘦身”:让你的网页快如闪电的烹饪秘籍
前端·vue.js
T___T19 分钟前
一个定时器,理清 JavaScript 里的 this
前端·javascript·面试
代码小学僧20 分钟前
从 Arco Table 迁移到 VTable:VTable使用经验分享
前端·react.js·开源
微笑的曙光21 分钟前
Vue3 环境搭建 5 步走(零基础友好)
前端
不知名用户来了25 分钟前
基于vue3 封装的antdv/element-Plus 快速生成增删改查页面
前端
明川31 分钟前
Android Gradle - ASM + AsmClassVisitorFactory插桩使用
android·前端·gradle
San3031 分钟前
深度驱动:React Hooks 核心之 `useState` 与 `useEffect` 实战详解
javascript·react.js·响应式编程
布列瑟农的星空32 分钟前
webpack迁移rsbuild——配置深度对比
前端
前端小黑屋33 分钟前
查看项目中无引用到的文件、函数
前端