Vue中的$router 和 $route的区别

点击视频讲解更加详细

this.$route:当前激活的路由的信息对象。每个对象都是局部的,可以获取当前路由的 path,
name, params, query 等属性。 this.$router:全局的 router 实例。通过 vue 根实例中注入 router 实例,然后再注入到每个
子组件,从而让整个应用都有路由功能。其中包含了很多属性和对象(比如 history 对象),任何
页面也都可以调用其 push(), replace(), go() 等方法。

路由跳转分为编程式和声明式

声明式:

简单来说,就是使用 router-link 组件来导航,通过传入 to 属性指定链接(router-link 默认会被渲染成一个a标签)。

编程式:

采用这种方式就需要导入 VueRouter 并调用了。

src\router\index.js

import Vue from 'vue';
import VueRouter from 'vue-router'; Vue.use(VueRouter) // 1. 定义一些路由
// 每个路由都需要映射到一个组件。
const routes = [
{ path: '/home', component: ()=> import('../views//home.vue') },
{ path: '/about', component: ()=> import('../views/about.vue') },
] const router = new VueRouter({
// mode: 'hash', //默认是hash模式,url是带#号的
mode: 'history', //history模式url不带#号
routes
}) export default router

src\views\home.vue

<template>
<div id="app">
<h1>home</h1>
<button @click="handerHerf">跳转</button>
</div>
</template>
<script>
export default {
name: 'App',
data(){
return { }
},
mounted() { },
components:{ },
methods:{
handerHerf(){
console.log('this.$router',this.$router)
this.$router.push('/about')
}
}
}
</script> <style scoped> </style>

src\views\about.vue

<template>
<div>
<h1>about</h1>
</div>
</template> <script>
export default {
name: 'about',
data(){
return { }
},
created(){
console.log('this.$route',this.$route)
},
mounted() { },
computed:{ },
methods:{ }
}
</script> <style scoped> </style>

this.$router参数详情

this.$route参数详情

若对您有帮助,请点击跳转到B站一键三连哦!感谢支持!!!

最新文章

  1. PHP审计小记
  2. oracle基础知识
  3. Linux IPC POSIX 共享内存
  4. canvas drawImage异步特性
  5. php脚本时 linux命令获取服务器IP
  6. 深入理解JVM—字节码执行引擎
  7. BS软件注册
  8. hihoCoder 1014trie树(字典树)
  9. JAVA开源爬虫,WebCollector,使用方便,有接口。
  10. 敏捷冲刺每日报告——Day2
  11. sql 用过记住
  12. LeetCode算法题-Maximum Depth of Binary Tree
  13. AppScan--图解Web扫描工具IBM Security App Scan Standard
  14. python 字符串输出转义{}
  15. Qimage QBuffer
  16. margin-bottom无效问题以及div里内容动态居中样式!
  17. 服务器上如何将D盘修改为E盘
  18. google学习
  19. DS导入导出命令详解
  20. [洛谷P2044][NOI2012]随机数生成器

热门文章

  1. Puppeteer学习笔记 (1)- 什么是Puppeteer
  2. 支持向量机SVM(一):基本概念、目标函数的推导
  3. 2 万字 + 20张图| 细说 Redis 九种数据类型和应用场景
  4. Opentelemetry SDK的简单用法
  5. RabbitMD大揭秘
  6. 11.2 Android Studio如何切换主题和更改字体
  7. java 配置aop 写入无效
  8. 关于webapi调用wcf并发假死的分析
  9. $\mathcal{A\,F\,O}$
  10. Linux(Centos7) 实例搭建 FTP 服务