404和路由钩子


404和路由钩子

路由模式有两种:

  • hash 路径带 #
  • histroy 路径不带 #

修改路由配置:

export default new Router({
    mode: 'history',
    routes: [
      {}
    ]
});

404配置

router配置

{
path: '*',  //匹配所有请求
component: NotFound
}

创建 NotFound.vue

<template>
  <div>
    <h1>  404 not found</h1>
  </div>
</template>

<script>
    export default {
        name: "NotFound"
    }
</script>

<style scoped>

</style>

当请求不在 path 中时,会显示 404 not found

路由钩子

<template>
<!-- 注意: 所有元素必须有标签包起来,不能直接暴露在根节点下-->
  <div>
    <h1>个人信息</h1>
    {{id}}
  </div>
</template>

<script>
    export default {
        props: ['id'],
        name: "userProfile",
        beforeRouteEnter: (to, from, next)=>{
            console.log("enter");
            next();
        },
        beforeRouteLeave: (to, from, next)=>{
            console.log("Leave");
            next();
        }
    }
</script>

<style scoped>

</style>

参数说明:

  • to, 路由将要跳转的路径信息
  • from, 路由跳转前的路径信息
  • next,路由的控制参数
    • next(),跳入下一个页面,放行
    • next(‘/path’),改变路由跳转方向,使其跳到另一个路由
    • next(false),返回原来的页面
    • next((vm)=>{}) 仅在beforeRouteEnter中可用,vm是实例

文章作者: Hailong Gao
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hailong Gao !
评论
  目录