博客
关于我
Vue的路由深入理解(重定向,嵌套,路由模式,异步)
阅读量:238 次
发布时间:2019-03-01

本文共 2588 字,大约阅读时间需要 8 分钟。

路由配置与组件传递

在 Vue 项目中,路由配置是构建单页应用的核心基础之一。通过合理配置路由,我们可以实现组件的嵌套、参数的传递以及动态路由的管理。本文将详细介绍路由嵌套、参数传递、组件转发、路由重定向、404错误处理、路由钩子以及异步请求的实现方法。

一、路由嵌套

嵌套路由(Sub-Route)是指在同一个路由路径下嵌套多个组件。这种结构在实际应用中非常常见,例如用户信息管理可能需要同时显示用户资料和相关操作菜单。以下是路由嵌套的实现方法:

  • 配置路由文件:在 router/index.js 中定义嵌套路由。以下是一个示例:

    export default new Router({  routes: [    { path: '/main', component: Main, children: [        { path: '/user/profile', name: 'UserProfile', component: UserProfile },        { path: '/user/list', name: 'UserList', component: UserList }      ] }    ]  ]});
  • 配置主视图组件:在 Main.vue 中,使用 ElementUI 的菜单组件来展示嵌套路由。以下是一个示例:

  • 二、参数传递

    在路由配置中,通常需要将动态路径参数传递给目标组件。例如,/user/profile/:id 这样的路径可以传递 id 参数。以下是实现方法:

  • 路由配置中添加占位符:在路由路径中使用 :id 等占位符。

    { path: '/user/profile/:id', component: UserProfile }
  • 通过 to 属性传递参数:在 router-link 组件中使用 :to 属性传递参数对象。

    个人信息
  • 接收参数:在目标组件中通过 $route.params 获取参数。

  • 三、组件转发

    在 Vue Router 中,组件之间的转发可以通过两种方式实现:$route 方法和 props 属性传递。

  • 使用 $route 方法

    在路由配置中,确保组件支持传递参数:

    { path: '/user/profile', component: UserProfile, props: true }
  • 使用 props 属性

    { path: '/user/profile', component: UserProfile, props: { id: true, name: true } }

    在目标组件中接收参数:

  • 四、路由重定向

    重定向是将一个路由路径指向另一个路由路径的过程。例如,可以将 /goHome 路径重定向到 /main 路径。

  • 配置路由文件

    { path: '/goHome', redirect: '/main' }
  • 在菜单中使用重定向

  • 五、404 错误处理

    在 Vue Router 中,可以配置一个全局的 404 错误页面,用于处理未匹配的路由请求。

  • 创建 NotFound.vue 组件

  • 配置路由文件

    export default new Router({  mode: 'history',  routes: [    { path: '*', component: NotFound }  ]});
  • 六、路由钩子与异步请求

    路由钩子(Route Hooks)是路由事件处理的函数,可以用于执行自定义逻辑。

  • 定义路由钩子

    export default {  name: 'UserProFile',  props: ['id', 'name'],  beforeRouteEnter: (to, from, next) => {    console.log('进入路由之前');    next();  },  beforeRouteLeave: (to, from, next) => {    console.log('进入路由之后');    next();  }};
  • 异步请求示例

    export default {  name: 'UserProFile',  props: ['id', 'name'],  beforeRouteEnter: (to, from, next) => {    next(vm => {      vm.getData();    });  },  methods: {    getData() {      this.axios({        method: 'get',        url: 'http://localhost:8080/static/mock/data.json'      }).then(response => {        console.log(response);      });    }  }};
  • 通过以上配置,我们可以实现路由的嵌套、参数传递、组件转发、重定向、错误处理以及异步请求等功能。在实际项目中,可以根据需求灵活配置路由文件和组件代码,确保应用的灵活性和可维护性。

    转载地址:http://tqux.baihongyu.com/

    你可能感兴趣的文章
    NOIp模拟赛二十九
    查看>>
    Vue3+element plus+sortablejs实现table列表拖拽
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>