路由对象route

  • 路由对象是不可变 (immutable) 的,每次成功的导航后都会产生一个新的对象。不过你可以 watch (监测变化) 它。
  • 通过 this.$route 访问当前路由,还可以通过router.match(location) 的返回值、导航守卫的参数、scrollBehavior 方法的参数访问路由对象
// Home.vue
export default {
  computed: {
    username () {
      // 我们很快就会看到 `params` 是什么
      return this.$route.params.username
    }
  },
  methods: {
    goBack () {
      window.history.length > 1
        ? this.$router.go(-1)
        : this.$router.push('/')
    }
  }
}

$route.path

  • 字符串,对应当前路由的路径,总是解析为绝对路径,如 "/foo/bar"

$route.params

  • 一个 key/value 对象,包含了动态片段(:id匹配的键值)和全匹配片段(?),如果没有路由参数,就是一个空对象。

$route.query

  • 一个 key/value 对象,表示 URL 查询参数。例如,对于路径 /foo?user=1,则有 $route.query.user == 1,如果没有查询参数,则是个空对象。

$route.hash

  • 当前路由的 hash 值 (带 #) ,如果没有 hash 值,则为空字符串。

$route.fullPath

  • 完成解析后的 URL,包含查询参数和 hash 的完整路径。

$route.matched

  • 一个数组,包含当前路由的所有嵌套路径片段的路由记录 。路由记录就是 routes 配置数组中的对象副本 (还包括 children 数组中的配置)。
  • 路由记录可以是嵌套的,因此,当一个路由匹配成功后,他可能匹配多个路由记录。一个路由匹配到的所有路由记录会暴露为 $route 对象的 $route.matched 数组
const router = new VueRouter({
  routes: [
    // 下面的对象就是路由记录
    { path: '/foo', component: Foo,
      children: [
        // 这也是个路由记录
        { path: 'bar', component: Bar }
      ]
    }
  ]
})

// 当 URL 为 /foo/bar,$route.matched 将会是一个包含从上到下的所有对象 (副本)。

$route.name

  • 当前路由的名称,如果有的话。

$route.redirectedFrom

  • 如果存在重定向,即为重定向来源的路由的名字。

猜你喜欢

转载自www.cnblogs.com/qq3279338858/p/10304044.html
今日推荐