how to create a 404 component in vuejs using vue-router

后端 未结 5 1034
慢半拍i
慢半拍i 2020-12-13 13:13

I\'m new to vuejs and I\'m working on my first project with vue. I\'m just wondering how I will route to my 404.vue component when the requested url is not found.

An

5条回答
  •  醉梦人生
    2020-12-13 13:33

    There are a couple of ways to do this.

    The most generic one is to check if the path matches any route before navigation and if not redirect to the Not found page.

    router.beforeEach((to, from, next) => {
      if (!to.matched.length) {
        next('/notFound');
      } else {
        next();
      }
    });
    

    See JSFiddle.

提交回复
热议问题