router路由简介与使用

简介: Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。路由实际上就是可以理解为指向,就是我在页面上点击一个按钮需要跳转到对应的页面,这就是路由跳转;


image.png

本教程为入门教程,如有错误,请各位前端大佬指出。

1.什么是路由

Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。路由实际上就是可以理解为指向,就是我在页面上点击一个按钮需要跳转到对应的页面,这就是路由跳转;

首先我们来学习三个单词(route,routes,router):

route:首先它是个单数,译为路由,即我们可以理解为单个路由或者某一个路由;

routes:它是个复数,表示多个的集合才能为复数;即我们可以理解为多个路由的集合,JS中表示多种不同状态的集合的形式只有数组和对象两种,事实上官方定义routes是一个数组;所以我们记住了,routes表示多个数组的集合;

router:译为路由器,上面都是路由,这个是路由器,我们可以理解为一个容器包含上述两个或者说它是一个管理者,负责管理上述两个;举个常见的场景的例子:当用户在页面上点击按钮的时候,这个时候router就会去routes中去查找route,就是说路由器会去路由集合中找对应的路由;

详细描述请参考官方文档router.vuejs.org/zh/guide/es…

2.安装和引用

1. npm install --save vue-router
2. 复制代码

在安装脚手架之后就生成了router/index.js。

这里需要修改router/index.js。

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import HelloWorld2 from '@/components/HelloWorld2'
Vue.use(Router)
export default new Router({
    routes: [{
        //路径
        path: '/hello',
        //引入名称
        name: 'HelloWorld',
        component: HelloWorld
    }, {
        //路径
        path: '/hello2',
        //引入名称
        name: 'HelloWorld2',
        component: HelloWorld2
    }]
})
复制代码

app.vue 然后在app.vue的,router标签中就会显示HelloWorld.vue。

<template>
  <div id="app">
  QWER
   <router-view></router-view>
  </div>
</template>
<script>
export default {
  name: 'App',
  data () {
  return {
 }
}
}
</script>
<style>
</style>
复制代码

然后分别访问 http://localhost:8080/#/, http://localhost:8080/#/hello, http://localhost:8080/#/hello2就可以跳转不同的页面了

3.项目中的简单使用

现在我们做一个点击不同按钮跳转不同页面的实验,app.vue是主入口,testlink为导航,根据testlink的导航,跳转到不同页面,router-view是根据导航显示不同的view。

app.vue

<template>
  <div id="app">
   QWER
   <TestLink />
   <router-view></router-view>
  </div>
</template>
<script>
import TestLink from "./components/TestLink.vue"
export default {
  name: 'App',
  data () {
  return {
 }
},
components:{
  TestLink,
}
}
</script>
<style>
</style>
复制代码

testlink.vue(/hello是路由中配置的 随便写两个页面配置路由)

<template>
<div>
   <ul>
    <li>
      <router-link tag = "p" to ="/hello">HelloWorld1</router-link>
    </li>
    <li>
      <router-link to ="/hello2">HelloWorld2</router-link>
    </li>
   </ul>
</div>
</template>
<script>
export default {
name: 'TestLink',
data () {
  return {
}
}
}
</script>
<style>
</style>
复制代码

重定向

重定向可以配置从/a到重定向/b。这里当访问"/"时,重定向到anim的路由。/ 完成这个功能需要修改router/index.js文件。

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import HelloWorld2 from '@/components/HelloWorld2'
import anim from '@/components/Anim'
Vue.use(Router)
export default new Router({
    routes: [
        //配置重定向--/的时候 跳到 anim路径
        {
            path: '/',
            redirect: "anim"
        },
        {
            //路径
            path: '/hello',
            //引入名称
            name: 'HelloWorld',
            component: HelloWorld
        }, {
            //路径
            path: '/hello2',
            //引入名称
            name: 'HelloWorld2',
            component: HelloWorld2
        },
        {
            //路径
            path: '/anim',
            //引入名称
            name: 'anim',
            component: anim
        }
    ]
})
复制代码

4.嵌套路由

一级路由下还有超链接,能够跳不同的页面,在以上代码的情况在,在进入HelloWorld页面后,还有两个超链接 能分别跳到TestNesting1和TestNesting2。以下代码实现嵌套路由的场景。

//TestNesting1两个随便的类 区别不同就可以了
<template>
<div>
   TestNesting1
</div>
</template>
<script>
export default {
name: 'TestNesting1',
data () {
  return {
}
}
}
</script>
复制代码

如上文一样 在HelloWorld中建立超链接 其中配置的是要跳入的路径加上本身路径。

<template>
<div>
      HelloWorld
      <ul>
       <li><router-link to = "/hello/TestNesting1">11111</router-link></li>
       <li><router-link to = "/hello/TestNesting2">22222</router-link></li>
      </ul>
      <router-view> </router-view>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
  return {
    }
},
methods:{
}
}
复制代码

配置路由文件

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import HelloWorld2 from '@/components/HelloWorld2'
import anim from '@/components/Anim'
import TestNesting1 from '@/components/TestNesting1'
import TestNesting2 from '@/components/TestNesting2'
Vue.use(Router)
export default new Router({
    routes: [{
            path: '/',
            redirect: "anim"
        },
        {
            //路径
            path: '/hello',
            //引入名称 传入参数一定要是用
            name: 'HelloWorld',
            component: HelloWorld,
            //TestNesting1的重定向
            //意味着在/hello时跳入/hello/TestNesting1 默认显示/hello/TestNesting1 首页功能
            redirect: "/hello/TestNesting1",
            //嵌套路由
            children: [
                // UserHome 会被渲染在 User 的 <router-view> 中
                {
                    //不要写/
                    path: 'TestNesting1',
                    component: TestNesting1
                },
                {
                    path: 'TestNesting2',
                    component: TestNesting2
                }
            ]
        }, {
            //路径
            path: '/hello2',
            //引入名称
            name: 'HelloWorld2',
            component: HelloWorld2
        },
        {
            //路径
            path: '/anim',
            //引入名称
            name: 'anim',
            component: anim
        }
    ]
})
复制代码

5.路由的参数传递

在跳转路由时,也可以传递参数。

首先在index.js配置参数 --需要在path后加入/:

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import HelloWorld2 from '@/components/HelloWorld2'
import anim from '@/components/Anim'
import TestNesting1 from '@/components/TestNesting1'
import TestNesting2 from '@/components/TestNesting2'
Vue.use(Router)
export default new Router({
    routes: [{
            path: '/',
            redirect: "anim"
        },
        {
            //路径
            path: '/hello',
            //引入名称 传入参数一定要是用
            name: 'HelloWorld',
            component: HelloWorld,
            //TestNesting1的重定向
            //意味着在/hello时跳入/hello/TestNesting1 默认显示/hello/TestNesting1 首页功能
            redirect: "/hello/TestNesting1",
            //嵌套路由
            children: [
                // UserHome 会被渲染在 User 的 <router-view> 中
                {
                    //不要写/
                    path: 'TestNesting1',
                    component: TestNesting1
                },
                {
                    path: 'TestNesting2',
                    component: TestNesting2
                }
            ]
        }, {
            //路径
            path: '/hello2/:id/:money',
            //引入名称
            name: 'HelloWorld2',
            component: HelloWorld2
        },
        {
            //路径
            path: '/anim',
            //引入名称
            name: 'anim',
            component: anim
        }
    ]
})
复制代码

在:to ="{name:'HelloWorld2',params:{id:'111',money:'3123123123'}}"中直接加入想要传递的参数即可。

<template>
<div>
   <ul>
    <li>
      <router-link to ="/anim">首页</router-link>
     </li>
    <li>
      <router-link :to ="test_router">HelloWorld1</router-link>
    </li>
    <li>
      <router-link :to ="{name:'HelloWorld2',params:{id:'111',money:'3123123123'}}">HelloWorld2</router-link>
    </li>
   </ul>
</div>
</template>
<script>
export default {
name: 'TestLink',
data () {
  return {
        test_router:"/hello"
}
}
}
</script>
<style>
</style>
复制代码

最后在被跳转页接收。

<template>
<div>
   HelloWorld2
   <p>传递的参数为{{$route.params.id}}+{{$route.params.money}}</p>
</div>
</template>
<script>
export default {
name: 'HelloWorld2',
data () {
  return {
}
}
}
</script>
复制代码

6.路由高亮

在router/index.js中加入linkActiveClass: "active"属性,他为所有的路由加入active的class,即可实现高亮。

export default new Router({
    //解决历史问题
    mode: 'history',
    //路由高亮
    linkActiveClass: "active",
    routes: [{
            path: '/',
            redirect: "anim"
        },
        {
            //路径
            path: '/hello',
            //引入名称 传入参数一定要是用
            name: 'HelloWorld',
            component: HelloWorld,
            //TestNesting1的重定向
            //意味着在/hello时跳入/hello/TestNesting1 默认显示/hello/TestNesting1 首页功能
            redirect: "/hello/TestNesting1",
            //嵌套路由
            children: [
                // UserHome 会被渲染在 User 的 <router-view> 中
                {
                    //不要写/
                    path: 'TestNesting1',
                    component: TestNesting1
                },
                {
                    path: 'TestNesting2',
                    component: TestNesting2
                }
            ]
        }, {
            //路径
            path: '/hello2/:id/:money',
            //引入名称
            name: 'HelloWorld2',
            component: HelloWorld2
        },
        {
            //路径
            path: '/anim',
            //引入名称
            name: 'anim',
            component: anim
        }
    ]
})
复制代码

然后在路由页面的css中加入以下代码,且指定颜色为红色。

1. <style>
2. .active{
3. color:red;
4. }
5. </style>


相关文章
|
9月前
|
机器学习/深度学习 SQL 自然语言处理
数据智能体技术路线深度对比:本体神经网络 vs 预制指标平台
本文剖析数据智能体四大技术路径:RAG(简单但精度低)、NL2SQL(单表准、多表差)、预制指标(高维护成本、扩展性差)、本体神经网络(UINO首创,95%+准确率,维护成本线性增长)。推荐企业优先选择本体论路线,实现高精准、低成本、强扩展的AI原生问数。
数据智能体技术路线深度对比:本体神经网络 vs 预制指标平台
|
XML JSON API
识别这些API接口定义(http,https,api,RPC,webservice,Restful api ,OpenAPI)
本内容介绍了API相关的术语分类,包括传输协议(HTTP/HTTPS)、接口风格(RESTful、WebService、RPC)及开放程度(API、OpenAPI),帮助理解各类API的特点与应用场景。
|
小程序 JavaScript 前端开发
微信小程序(十七)小程序监听返回键跳转事件(安卓返回也适用)
onUnload:function(){ wx.redirectTo({ url: '../index/index' }) wx.navigateTo({ url: '../index/index' }) wx.switchTab({ url: '../../member/member' }) }
2188 0
|
算法 Java 程序员
Java垃圾回收机制详解
Java垃圾回收机制详解
236 0
|
数据采集 数据可视化 数据挖掘
cbind与rbind:网页爬取数据的合并策略
短视频数据爬取与合并简介 随着短视频平台的兴起,快手等平台成为信息传播的重要载体。本文探讨如何使用Python爬取并分析快手视频数据,重点介绍cbind和rbind两种数据合并方法。通过代理IP、自定义User-Agent和Cookie配置,以及多线程技术,提高爬取效率和突破率。代码示例展示了如何抓取视频简介和评论,并将其合并为结构化表格,助力高效数据分析。 关键点: 代理IP:避免被限制。 User-Agent和Cookie:增加请求成功率。 多线程:提升处理速度。 cbind和rbind:增强数据完整性和可视化效果。 该方案适用于大量网站数据的高效获取与处理,为数据分析提供有力支持。
456 8
cbind与rbind:网页爬取数据的合并策略
|
SQL 分布式计算 DataWorks
DataWorks智能交互式数据开发与分析之旅
本次实验将带您进行DataWorks Notebook的快速入门,包含:Notebook新建、多引擎SQL开发与分析、Python开发、交互式分析等,同时,使用DataWorks Copilot体验智能数据开发,体验智能交互式数据探索之旅。
3563 11
|
小程序
uniapp如何隐藏默认的页面头部导航栏,uniapp开发小程序如何隐藏默认的页面头部导航栏
uniapp如何隐藏默认的页面头部导航栏,uniapp开发小程序如何隐藏默认的页面头部导航栏
1709 0
|
Ubuntu TensorFlow 算法框架/工具
NVIDIA Triton系列05-安装服务器软件
本文介绍了NVIDIA Triton推理服务器的安装方法,涵盖源代码编译、可执行文件安装及Docker容器部署三种方式。重点讲解了在NVIDIA Jetson AGX Orin设备上的安装步骤,适合Ubuntu 18及以上系统。通过检查HTTP端口状态确认服务器运行正常,为后续客户端软件安装做准备。
1693 0
NVIDIA Triton系列05-安装服务器软件