Solution to the problem of repeated triggering of functions in Vue project watch

Solution to the problem of repeated triggering of functions in Vue project watch

Problem description:

There are two pages A and B, each page has a getList() method. Both methods need to pass the same parameter C, and the process of selecting parameter C is rather complicated. In order to avoid the problem of repeatedly selecting parameter C when switching between interfaces A and B, I store parameter C in vuex, and then use watch to listen to parameter C in both pages to execute getList() method. Then I found a problem. After entering page B from page A, if I reselect parameter C on page B, the getList() method of page A will also be executed. Vice versa, after going from page B to page A, if I change parameter C on page A, the getList() method of page B will also be executed.

Later I found out that it was because of the use of keep-alive. Keep-alive will always keep the Vue instance in memory, so the Vue instance always exists and the corresponding watchers are always effective. After searching for relevant information, I found that many people have encountered this problem. Finally, I found the following two solutions:

Solution 1

Determine whether to execute getList() by the router path

watch:
        someValue(newValue, oldValue) {
            if (this.$route.fullPath === 'A page routing path') {
                // do something
            }
        }
    }

Solution 2

Add a flag parameter to determine whether the page is in the active state. Components using keep-alive caching will only trigger activated and deactivated events, so set the flag to true and false when these two events are triggered. Only when the flag is true will getList() be executed.

{
  data () {
    return {
      activatedFlag: false
    };
  },
  watch:
    'someValue'(val) {
      if(val && this.activatedFlag) {
        this.getlist();
      }
    }
  },
  activated () {
    this.activatedFlag = true;
  },
  deactivated () {
    this.activatedFlag = false;
  }
}

If there are many pages and the function names in each page are inconsistent, you can remove the watch part of the above code and write it into a mixin, and then import it on the required page.

  import activeFlag from "@/mixin/activeFlag";

  export default {
    mixins: [activeFlag],
    watch:
        'someValue'(val) {
          if(val && this.activatedFlag) {
            this.getlistA();
            this.getlistB();
          }
        }
      },
  }

The above are the details of the two solutions to the problem of repeated triggering of functions in the vue project watch. For more information on the solution to repeated triggering of vue watch functions, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Solution to automatically trigger click events when clicking on pop-up window in Vue (simulation scenario)
  • Solve the triggering problem of change event when vue elementUI uses el-select
  • Detailed explanation of the event of triggering child components by clicking buttons in parent components in Vue
  • Vue prevents multiple triggering requests after continuous clicks in a short period of time
  • A brief discussion on the triggering conditions of vue.watch
  • How to realize automatic triggering function in Vue

<<:  How to install multiple mysql5.7.19 (tar.gz) files under Linux

>>:  Weird and interesting Docker commands you may not know

Recommend

Ubuntu Server Installation Tutorial in Vmware

This article shares with you the Ubuntu server ve...

Teach you how to get the pointer position in javascript

The method of obtaining the position of the point...

Detailed explanation of Grid layout and Flex layout of display in CSS3

Gird layout has some similarities with Flex layou...

Vue uses mockjs to generate simulated data case details

Table of contents Install mockjs in your project ...

mysql8.0 windows x64 zip package installation and configuration tutorial

MySQL 8 Windows version zip installation steps (d...

The difference between html, xhtml and xml

Development Trends: html (Hypertext Markup Languag...

33 ice and snow fonts recommended for download (personal and commercial)

01 Winter Flakes (Individual only) 02 Snowtop Cap...

Cross-database association query method in MySQL

Business scenario: querying tables in different d...

Detailed explanation of data types and schema optimization in MySQL

I'm currently learning about MySQL optimizati...

The process of deploying and running countly-server in docker in win10

I have just come into contact with and become fam...

An example of how to quickly deploy web applications using Tomcat in Docker

After learning the basic operations of Docker, we...