Skip to content

How to maximize the reuse of code in beforeRouteEnter and beforeRouteUpdate #2113

Closed
@rubyLiuqq

Description

@rubyLiuqq

Question Description

Data is requested in the beforeRouteEnter, but when the route is changed, the beforeRouteUpdate function will be triggered. At this point, the code in the two functions is almost the same. Before this method can't be read in the beforeRouteEnter, how should I optimize the extraction code?

Brief Code

beforeRouteEnter(to, _from, next) {
      axios.all([
        axios.get(`/member?current=${to.query.page}`),
        axios.get(`/axiosUrl1`),
      ]).then(( data ) => {
        next((vm) => {
          vm.$set(vm, 'data', data.xxxx);
          _...........(your code)_
        });
      });
    }

And now, When the route changes

beforeRouteUpdate(to, _from, next) {
     axios.all([
       axios.get(`/member?current=${to.query.page}`),
       axios.get(`/axiosUrl1`),
     ]).then(( data ) => {
       this.data = data.xxxx;
       next();
     });
}

The axios request is exactly the same, but the set value is different. You can't extract the promise function in methods because beforeRouteEnter needs to call the methods in the methods in vm, which loses the purpose of requesting data using the beforeRouteEnter hook function.
What can I do to make the code achieve maximum reuse

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions