• Vue--Router--解决watch监听路由无效的问题


    原文网址:Vue--Router--解决watch监听路由无效的问题_IT利刃出鞘的博客-CSDN博客

    简介

    说明

            本文用实例介绍如何解决watch监听路由无效的问题。

    需求

            有两个组件:CompA和CompB,它们对应的path分别是:/compA、/compB。CompA组件引入CompB组件,并通过router-link跳转到B组件。想在CompA和CompB两个组件中打印出路由跳转的日志。

    问题复现

    代码

    路由(router/index.js)

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import CompA from '../components/CompA'
    4. import CompB from '../components/CompB'
    5. Vue.use(VueRouter)
    6. const routes = [
    7. {
    8. path: '/compA',
    9. name: 'CompA',
    10. component: CompA
    11. },
    12. {
    13. path: '/compB',
    14. name: 'CompB',
    15. component: CompB
    16. }
    17. ]
    18. const router = new VueRouter({
    19. routes
    20. })
    21. export default router

    A组件

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是CompA
    5. div>
    6. <router-link :to="{name:'CompB'}">跳转到CompBrouter-link>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name: 'CompA',
    12. watch: {
    13. $route: {
    14. handler (to, from) {
    15. console.log('组件:' + this.$options.name)
    16. console.log('来自:' + from.name)
    17. console.log('去往:' + to.name)
    18. }
    19. }
    20. // 也可以这么写:
    21. // $route (to, from) {
    22. // console.log('组件:' + this.$options.name)
    23. // console.log('来自:' + from.name)
    24. // console.log('去往:' + to.name)
    25. // }
    26. }
    27. }
    28. script>
    29. <style scoped>
    30. .outer {
    31. margin: 20px;
    32. border: 2px solid blue;
    33. padding: 20px;
    34. }
    35. style>

    B组件

    1. <template>
    2. <div class="outer">
    3. 这是CompB
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'CompB',
    9. watch: {
    10. $route: {
    11. handler (to, from) {
    12. console.log('组件:' + this.$options.name)
    13. console.log('来自:' + from.name)
    14. console.log('去往:' + to.name)
    15. }
    16. }
    17. }
    18. }
    19. script>
    20. <style scoped>
    21. .outer {
    22. margin: 20px;
    23. border: 2px solid blue;
    24. padding: 20px;
    25. }
    26. style>

    测试

    访问:http://localhost:8080/#/compA

    可以发现:没有打印任何路由变化的日志!

    解决方案

    方案1:immediate: true

    watch的属性:immediate: true  //一旦监听到路由的变化立即执行

    代码

    只修改watch部分,改为:

    1. watch: {
    2. $route: {
    3. handler (to, from) {
    4. console.log('组件:' + this.$options.name)
    5. console.log('来自:' + from)
    6. console.log('去往:' + to.name)
    7. },
    8. immediate: true
    9. }
    10. }

    测试

    访问:http://localhost:8080/#/compA

    可以发现:子路由之间的跳转只能获得目的路由,无法获得来源路由。 

    方案2:路由包含子路由

    代码

    路由设置(router/index.js)

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Parent from '../components/Parent'
    4. import CompA from '../components/CompA'
    5. import CompB from '../components/CompB'
    6. Vue.use(VueRouter)
    7. const routes = [
    8. {
    9. path: '/',
    10. name: 'Parent',
    11. component: Parent,
    12. children: [
    13. {
    14. path: 'compA',
    15. name: 'CompA',
    16. component: CompA
    17. },
    18. {
    19. path: 'compB',
    20. name: 'CompB',
    21. component: CompB
    22. }
    23. ]
    24. }
    25. ]
    26. const router = new VueRouter({
    27. routes
    28. })
    29. export default router

    父组件(components/Parent.vue)

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是CompA
    5. div>
    6. <router-link :to="{name:'CompB'}">跳转到CompBrouter-link>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name: 'CompA',
    12. watch: {
    13. $route: {
    14. handler (to, from) {
    15. console.log('组件:' + this.$options.name)
    16. console.log('来自:' + from.name)
    17. console.log('去往:' + to.name)
    18. }
    19. }
    20. }
    21. }
    22. script>
    23. <style scoped>
    24. .outer {
    25. margin: 20px;
    26. border: 2px solid blue;
    27. padding: 20px;
    28. }
    29. style>

    子组件(components/CompA.vue)

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是CompA
    5. div>
    6. <router-link :to="{name:'CompB'}">跳转到CompBrouter-link>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name: 'CompA',
    12. watch: {
    13. $route: {
    14. handler (to, from) {
    15. console.log('组件:' + this.$options.name)
    16. console.log('来自:' + from.name)
    17. console.log('去往:' + to.name)
    18. }
    19. }
    20. }
    21. }
    22. script>
    23. <style scoped>
    24. .outer {
    25. margin: 20px;
    26. border: 2px solid blue;
    27. padding: 20px;
    28. }
    29. style>

    子组件(components/CompB.vue)

    1. <template>
    2. <div class="outer">
    3. 这是CompB
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'CompB',
    9. watch: {
    10. $route: {
    11. handler (to, from) {
    12. console.log('组件:' + this.$options.name)
    13. console.log('来自:' + from.name)
    14. console.log('去往:' + to.name)
    15. }
    16. }
    17. }
    18. }
    19. script>
    20. <style scoped>
    21. .outer {
    22. margin: 20px;
    23. border: 2px solid blue;
    24. padding: 20px;
    25. }
    26. style>

    测试

    测试1:CompA跳转CompB

    访问:http://localhost:8080/#/compA

    可以发现:子路由之间的跳转可以获得来源路由和目的路由。 

    测试2:Parent跳转CompA,CompA跳转CompB

    访问:http://localhost:8080/#/

    可以发现:父=> 子、子=> 子、子=> 父,这三个之间的跳转可以获得来源路由和目的路由。 

  • 相关阅读:
    『第四章』一见倾心:初识小雨燕(上)
    建模杂谈系列155 从一段程序讨论通用的任务执行方法
    第三十七章 在 UNIX®、Linux 和 macOS 上使用 IRIS(二)
    偏向锁,轻量级锁,重量级锁的核心原理
    python计算机毕业设计基于django的空闲教室爬虫系统
    TypeError: ‘dict‘ object is not callable in Python
    【MySQL】库和表的操作
    图扑数字孪生智慧加油站,构建安全防护网
    TCP协议之《乱序队列Out-Of-Order》
    《Vue.js+Spring Boot全栈开发实战》简介
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126275930