vue-router基础

vue-router的基本概念及用法

动态路由匹配

1、把某种模式匹配到的所有路由,全都映射到同个组件

// 动态路径参数 以冒号开头
{ path: '/user/:id', component: User }
像 /user/foo 和 /user/bar 都将映射到相同的路由。
一个“路径参数”使用冒号 : 标记。当匹配到一个路由时,参数值会被设置到 this.$route.params,可以在每个组件内使用

2、捕获所有路由或 404 Not found 路由
{
  // 会匹配所有路径
  path: '*'
}
{
  // 会匹配以 `/user-` 开头的任意路径
  path: '/user-*'
}


嵌套路由

在 User 组件的模板添加一个 <router-view>:
const User = {
  template: `
    <div class="user">
      <h2>User {{ $route.params.id }}</h2>
      <router-view></router-view>
    </div>
  `
}

const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User,
      children: [
        {
          // 当 /user/:id/profile 匹配成功,
          // UserProfile 会被渲染在 User 的 <router-view> 中
          path: 'profile',
          component: UserProfile
        },
        {
          // 当 /user/:id/posts 匹配成功
          // UserPosts 会被渲染在 User 的 <router-view> 中
          path: 'posts',
          component: UserPosts
        }
      ]
    }
  ]
})
基于上面的配置,当你访问 /user/foo 时,User 的出口是不会渲染任何东西,这是因为没有匹配到合适的子路由
如果你想要渲染点什么,可以提供一个 空的 子路由:
const router = new VueRouter({
  routes: [
    {
      path: '/user/:id', component: User,
      children: [
        // 当 /user/:id 匹配成功,
        // UserHome 会被渲染在 User 的 <router-view> 中
        { path: '', component: UserHome },

        // ...其他子路由
      ]
    }
  ]
})

编程式导航

除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现
在 Vue 实例内部,你可以通过 $router 访问路由实例。因此你可以调用 this.$router.push。

router.push('home') // 字符串  
router.push({ path: 'home' }) // 对象  
router.push({ name: 'user', params: { userId: '123' }}) // 命名的路由 /user/123
router.push({ path: 'register', query: { plan: 'private' }}) // 带查询参数,变成 /register?plan=private


router.replace() // 跟 router.push 很像, 唯一的不同就是,它不会向 history 添加新记录
router.go(n)  // 参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似
 back 回退一步
 forward 前进一步
 go 指定前进或回退步数;
 push 导航到不同的URL,向history 栈添加一个新的记录;
 replace 导航到不同的URL,替换 history 栈中当前记录;


命名路由

name 为路由的名字
const router = new VueRouter({
  routes: [
    {
      path: '/user/:userId',
      name: 'user',
      component: User
    }
  ]
})

<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>

命名视图

有时候想同时 (同级) 展示多个视图,而不是嵌套展示,
有 sidebar (侧导航) 和 main (主内容) 两个视图,这个时候命名视图就派上用场了
在界面中拥有多个单独命名的视图,而不是只有一个单独的出口
如果 router-view 没有设置名字,那么默认为 default。

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>

const router = new VueRouter({
  routes: [
    {
      path: '/',
      components: {
        default: Foo,
        a: Bar,
        b: Baz
      }
    }
  ]
})

2、 嵌套命名视图 helper名称对于了路由的中的helper
<div>
  <h1>User Settings</h1>
  <NavBar/>
  <router-view/>
  <router-view name="helper"/>
</div>

{
  path: '/settings',
  // 你也可以在顶级路由就配置命名视图
  component: UserSettings,
  children: [{
    path: 'emails',
    component: UserEmailsSubscriptions
  }, {
    path: 'profile',
    components: {
      default: UserProfile,
      helper: UserProfilePreview
    }
  }]
}

重定向和别名

1、重定向
当访问 /a 重定向到 /b:
  routes: [
    { path: '/a', redirect: '/b' }
  ]

重定向的目标也可以是一个命名的路由:
   routes: [
    { path: '/a', redirect: { name: 'foo' }}
  ]
甚至是一个方法,动态返回重定向目标:
const router = new VueRouter({
  routes: [
    { path: '/a', redirect: to => {
      // 方法接收 目标路由 作为参数
      // return 重定向的 字符串路径/路径对象
    }}
  ]

2、
“重定向”的意思是,当用户访问 /a时,URL 将会被替换成 /b,然后匹配路由为 /b,
/a 的别名是 /b,意味着,当用户访问 /b 时,URL 会保持为 /b,但是路由匹配则为 /a,就像用户访问 /a 一样。
alias 字段配置别名
const router = new VueRouter({
  routes: [
    { path: '/a', component: A, alias: '/b' }
  ]
})


路由组件传传参

在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性。
const User = {
  props: ['id'],
  template: '<div>User {{ id }}</div>'
}
const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User, props: true },

    // 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
    {
      path: '/user/:id',
      components: { default: User, sidebar: Sidebar },
      props: { default: true, sidebar: false }
    }
  ]
})
1、布尔模式
如果 props 被设置为 true,route.params 将会被设置为组件属性

2、对象模式  
如果 props 是一个对象,它会被按原样设置为组件属性。当 props 是静态的时候有用。

3、函数模式
你可以创建一个函数返回 props。这样你便可以将参数转换成另一种类型,将静态值与基于路由的值结合等等。

const router = new VueRouter({
  routes: [
    { path: '/search', component: SearchUser, props: (route) => ({ query: route.query.q }) }
  ]
})

History 模式

vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。

如果不想要很丑的 hash,我们可以用路由的 history 模式,
这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。




posted @ 2021-01-03 15:59  kgwei  阅读(122)  评论(0编辑  收藏  举报