search

Home  >  Q&A  >  body text

javascript - How does vue-router distinguish between to and from in watch?

created () {
    this.getData()
},
watch: {
    $route (to, from) {
        // 执行ajax请求,但只希望在进入时请求,离开时不希望进行请求。
        this.getData()
    }
}

The current situation is that the router makes requests when entering or leaving. How to solve unnecessary requests?

Thanks you in advance.

滿天的星座滿天的星座2821 days ago581

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:48:34

    beforeRouteEnter is better?

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:48:34

    Use routing hook function

    beforeEnter
    

    or component hook function

    beforeRouteEnter

    reply
    0
  • Cancelreply