Skip to content

How notify on vue router beforeRouteEnter? #27

Open
@jgrandar

Description

@jgrandar

I have a component with beforeRouteEnter() to fetch some data with AXIOS before navigate and init the component like so:

// Post.vue
beforeRouteEnter(to, from, next) {
      axios.get(`/posts/${to.params.postId}`)
        .then((response) => {
          next((vm) => {
            vm.setPost(response.data);
          });
        })
        .catch((error) => {
          next(false);
          // Now I need to notify the error with snotify.
        });
    },

I know in this situation I don't have access to the component as it has not been created yet. I also tried in the router instance file but I am in the same situation.

// Post.vue
beforeRouteEnter(to, from, next) {
      axios.get(`/posts/${to.params.postId}`)
        .then((response) => {
          next((vm) => {
            vm.setPost(response.data);
          });
        })
        .catch((error) => {
          const errorJS = new Error(error.data);
          next(errorJS);
        });
    },
// index.js
Vue.use(Router);

const router = new Router({
    mode,
    routes,
});

router.onError((error) => {
  // eslint-disable-next-line
  console.log(error);
  // Same situation here, either of following doesn't work.
  vm.$snotify...
  Vue.$snotify....;
});

export default router;

How can I notify when user can't access to the route?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions