ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【面试题】Vue中的$router 和 $route的区别

2022-08-28 10:01:26  阅读:232  来源: 互联网

标签:面试题 Vue route about vue VueRouter router 路由


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站一键三连哦!感谢支持!!!

标签:面试题,Vue,route,about,vue,VueRouter,router,路由
来源: https://www.cnblogs.com/mochenxiya/p/16632281.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有