资讯专栏INFORMATION COLUMN

手把手教你学Vue-3(路由)

SolomonXie / 1850人阅读

摘要:记得要通过配置参数注入路由,从而让整个应用都有路由功能动态路由一个路径参数使用冒号标记。当匹配到一个路由时,参数值会被设置到,可以在每个组件内使用。

1.路由的作用

1.当我们有多个页面的时候 ,我们需要使用路由,将组件(components)映射到路由(routes),然后告诉 vue-router 在哪里渲染它们。

</>复制代码

  1. 简单的路由

</>复制代码

  1. const routes = [
  2. { path: "/foo", component: Foo },
  3. { path: "/bar", component: Bar }
  4. ]
  5. // 3. 创建 router 实例,然后传 `routes` 配置
  6. // 你还可以传别的配置参数, 不过先这么简单着吧。
  7. const router = new VueRouter({
  8. routes // (缩写)相当于 routes: routes
  9. })
  10. // 4. 创建和挂载根实例。
  11. // 记得要通过 router 配置参数注入路由,
  12. // 从而让整个应用都有路由功能
  13. const app = new Vue({
  14. router
  15. }).$mount("#app")

</>复制代码

  1. 动态路由
    一个『路径参数』使用冒号 : 标记。当匹配到一个路由时,参数值会被设置到 this.$route.params,可以在每个组件内使用。于是,我们可以更新 User 的模板,输出当前用户的 ID:

</>复制代码

  1. const User = {
  2. template: "
    User
    "
  3. }
  4. const router = new VueRouter({
  5. routes: [
  6. // 动态路径参数 以冒号开头
  7. { path: "/user/:id", component: User }
  8. ]
  9. })

</>复制代码

  1. 潜套路由

</>复制代码

  1. const router = new VueRouter({
  2. routes: [
  3. { path: "/user/:id", component: User,
  4. children: [
  5. {
  6. // 当 /user/:id/profile 匹配成功,
  7. // UserProfile 会被渲染在 User 的
  8. path: "profile",
  9. component: UserProfile
  10. },
  11. {
  12. // 当 /user/:id/posts 匹配成功
  13. // UserPosts 会被渲染在 User 的
  14. path: "posts",
  15. component: UserPosts
  16. }
  17. ]
  18. }
  19. ]
  20. })

子节点的router 会渲染到父节点User router-view 里面

router.push、 router.replace 和 router.go
想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

</>复制代码

  1. router-link :to="{ name: "user", params: { userId: 123 }}">User
  2. router.push({ name: "user", params: { userId: 123 }})
2.命名视图

一个 layout 布局展示

</>复制代码

  1. const router = new VueRouter({
  2. routes: [
  3. {
  4. path: "/",
  5. components: {
  6. default: Foo,
  7. a: Bar,
  8. b: Baz
  9. }
  10. }
  11. ]
  12. })

命名视图中我们需要注意的地方,在props路由传值上面 对于包含命名视图的路由,你必须分别为每个命名视图添加 props 选项

</>复制代码

  1. const User = {
  2. props: ["id"],
  3. template: "
    User {{ id }}
    "
  4. }
  5. const router = new VueRouter({
  6. routes: [
  7. { path: "/user/:id", component: User, props: true },
  8. // 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
  9. {
  10. path: "/user/:id",
  11. components: { default: User, sidebar: Sidebar },
  12. props: { default: true, sidebar: false }
  13. }
  14. ]
  15. })
3.深入理解路由

</>复制代码

  1. 路由的配置
  2. declare type RouteConfig = {
  3. path: string;
  4. component?: Component;
  5. name?: string; // 命名路由
  6. components?: { [name: string]: Component }; // 命名视图组件
  7. redirect?: string | Location | Function;
  8. props?: boolean | string | Function;
  9. alias?: string | Array;
  10. children?: Array; // 嵌套路由
  11. beforeEnter?: (to: Route, from: Route, next: Function) => void;
  12. meta?: any;
  13. // 2.6.0+
  14. caseSensitive?: boolean; // 匹配规则是否大小写敏感?(默认值:false)
  15. pathToRegexpOptions?: Object; // 编译正则的选项
  16. }

后面实际应用中,在补充一下文档----目前都是看官方文档

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/54544.html

相关文章

  • 把手你学Vue-3(路由)

    摘要:记得要通过配置参数注入路由,从而让整个应用都有路由功能动态路由一个路径参数使用冒号标记。当匹配到一个路由时,参数值会被设置到,可以在每个组件内使用。 1.路由的作用 1.当我们有多个页面的时候 ,我们需要使用路由,将组件(components)映射到路由(routes),然后告诉 vue-router 在哪里渲染它们。 简单的路由 const routes = [ { path: ...

    Prasanta 评论0 收藏0
  • 把手你学Vue-3(路由)

    摘要:记得要通过配置参数注入路由,从而让整个应用都有路由功能动态路由一个路径参数使用冒号标记。当匹配到一个路由时,参数值会被设置到,可以在每个组件内使用。 1.路由的作用 1.当我们有多个页面的时候 ,我们需要使用路由,将组件(components)映射到路由(routes),然后告诉 vue-router 在哪里渲染它们。 简单的路由 const routes = [ { path: ...

    laznrbfe 评论0 收藏0
  • 把手你学Dapr

    摘要:配置配置使用概率抽样。采样率定义了对跟踪跨度进行采样的概率,其值可以介于和含之间。例如,以下配置对象将采样率更改为即每个跨度都被采样,并使用协议将跟踪发送到位于的服务器文件路径注将采样率更改为会完全禁用跟踪。目录手把手教你学Dapr - 1. .Net开发者的大时代手把手教你学Dapr - 2. 必须知道的概念手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序手把手教你学Da...

    qqlcbb 评论0 收藏0
  • 把手你学Vue-2(组件开发)

    摘要:组件声明组件分为全局的和局部的。父组件通过给子组件下发数据,子组件通过事件给父组件发送消息。以下实例中子组件已经和它外部完全解耦了。 1.vue 组件-声明 组件分为全局的和局部的。 全局声明 Vue.component(tagName, options) ** 引用组件 // 注册 Vue.comp...

    lansheng228 评论0 收藏0
  • 把手你学Vue-2(组件开发)

    摘要:组件声明组件分为全局的和局部的。父组件通过给子组件下发数据,子组件通过事件给父组件发送消息。以下实例中子组件已经和它外部完全解耦了。 1.vue 组件-声明 组件分为全局的和局部的。 全局声明 Vue.component(tagName, options) ** 引用组件 // 注册 Vue.comp...

    Null 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<