Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rubyLiuqq opened this issue Mar 19, 2018 · 2 comments
Closed

Comments

@rubyLiuqq
Copy link

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

@vue-bot
Copy link

vue-bot commented Mar 19, 2018

Hello, your issue has been closed because it does not conform to our issue requirements. In order to ensure every issue provides the necessary information for us to investigate, we require the use of the Issue Helper when creating new issues. Thank you!

@vue-bot vue-bot closed this as completed Mar 19, 2018
@rubyLiuqq
Copy link
Author

To see #1582

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants