一个小demo带你体验vue-router的魔力

前言

下面为该demo的最终效果:

vue中所有的xxx.vue文件都是一个组件,这些组件最终会被vue读取,并编译成一段div结构,挂载在唯一的html文件中,所以想要实现组件之间的切换很简单。 但是,想要将某些组件当成页面(每个页面对应的url都是唯一的)来用,默认情况下就行不通,那么,我们就需要一个可以将代码片段的切换模拟成页面的跳转的样子的这样的一个手段,这就是vue-router

下面来介绍小demo吧~

正文

准备

  • 生成vue项目:在终端输入npm create vue@latest
  • 安装包,在终端输入npm i

先介绍一下第一排的三个vue文件吧:

(快捷方式"输入vb3s按回车键,如果没用的话,应该是没安装插件,安装一个Vue VSCode Snippet插件)

  • home.vue:
xml 复制代码
<template>
    <div>
       <h2>home page</h2>

       <div class="nav">
         <router-link to="/home/newest">最新</router-link>
         <router-link to="/home/suggest">推荐</router-link>
       </div>

       <router-view></router-view>
    </div>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>
  • bot.vue:

router.push({path: '/hot', query: {id: 1, name: 'yezhi'}});:后面的query会传递到网址链接上

xml 复制代码
<template>
    <div>
      <p>BOT page</p>
      <button @click="goHot">去沸点</button>
    </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

const goHot = () => {
// 跳转到沸点页面
// router.push('/hot');
router.push({path: '/hot', query: {id: 1, name: 'yezhi'}});
};
</script>

<style lang="scss" scoped>

</style>
  • hot.vue:
xml 复制代码
<template>
    <div>
       hot page
    </div>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>

home.vue的页面里还有两个子文件,再新建一个home文件夹:

  • newest.vue:
xml 复制代码
<template>
    <div>
       最新的数据
    </div>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>
  • suggest.vue:
xml 复制代码
<template>
    <div>
       推荐的文章
    </div>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>

所有的vue文件已经介绍完了,现在我们来配置路由,实现跳转:

  • 新建一个router文件夹,在里面新建一个index.js文件,在该文件里配置路由:
  1. 定义一个routes,里面存放路由,每一个对象里有两个属性pathcomponent,子组件通过children来存放;
css 复制代码
import Home from '../pages/home.vue';

{
    path: '/home',
    component: Home,
    children: [
      {
        path: '/home',
        redirect:'/home/suggest'
      },
      {
        path: '/home/newest',
        component: () => import('../pages/home/newest.vue')
      },
      {
        path: '/home/suggest',
        component: () => import('../pages/home/suggest.vue')
      }
    ]
  },

还可以直接用一个函数,通过import来配置路由

css 复制代码
 {
    path: '/hot',
    component:() => import('../pages/hot.vue')
  }
  1. 如果想要一打开就是某个页面的话,通过一个对象,该对象中有两个属性pathredirect:根路径为home的界面,也就是一打开就是home
css 复制代码
{
    path: '/',
    redirect: '/home'
}
  1. 创建一个路由
php 复制代码
import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
    history: createWebHistory(),
    routes: routes
});
  1. 导出为默认路由
arduino 复制代码
export default router;
  1. index.js总代码:
javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../pages/home.vue';
import Bot from '../pages/bot.vue';
import Hot from '../pages/hot.vue';

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '/home',
        redirect:'/home/suggest'
      },
      {
        path: '/home/newest',
        component: () => import('../pages/home/newest.vue')
      },
      {
        path: '/home/suggest',
        component: () => import('../pages/home/suggest.vue')
      }
    ]
  },
  {
    path: '/bot',
    component: Bot
  },
  {
    path: '/hot',
    component:() => import('../pages/hot.vue')
  }
];

const router = createRouter({
    history: createWebHistory(),
    routes: routes
});

export default router;
  • main.js内引入上面的index.js文件
javascript 复制代码
import router from './router/index.js'
  • App.vue文件中放一个 <router-view>标签用于展示vue文件,用router-link来实现路由跳转
xml 复制代码
<template>
  <div>
     <nav>
      <router-link to="/home">首页</router-link>|
      <router-link to="/bot">BOT</router-link>|
      <router-link to="/hot">沸点</router-link>
     </nav>

     <!-- 页面 -->
     <router-view></router-view>
  </div>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>

运行

在终端输入npm run dev

结语

相关推荐
Vicky&James5 分钟前
WPF到Web的无缝过渡:英雄联盟客户端项目OpenSilver迁移实战
前端·wpf
m0_748233649 分钟前
RabbitMQ 进阶
android·前端·后端
不想有bug的小菜鸟17 分钟前
vue3使用iframe全屏展示pdf效果
前端·pdf
m0_7482386318 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
u01005596019 分钟前
前端代理,解决跨域问题讲解
前端
quitv24 分钟前
react脚手架配置别名
前端·javascript·react.js
m0_5287238133 分钟前
前端如何进行性能优化
前端·性能优化
化作繁星34 分钟前
在 Vue 3 中,如何缓存和复用动态组件
前端·vue.js·缓存
一粒沙-1 小时前
iOS 将GIF图分享至微信
前端·ios
graywen1 小时前
从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
前端