vue-router

  1. // mode 有 hash模式 和 history 模式
    //默认 hash(#) 模式 : 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。
    //history 模式 :
    由于hash模式会在url中自带#,如果不想要很丑的 hash,我们可以用路由的 history 模式,只需要在配置路由规则时,
    // 加入"mode: 'history'",这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。

    export default new Router({ mode: 'history', routes: [ { path: '/home', name: 'home', component: Home } ] })
  2.  如果想要跳转到某个页面:
this.$router.push({path: '/home'})

但是要先确保此路径已经被添加到 router 中。    位置 /src/router/index.js 

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/home/home.vue'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/home',  //url
      name: 'home',   // name 名称
      component: Home // 对应 import Home from '@/components/home/home.vue' 中的 Home
}
]
})

如果该页面 url 中有参数 : 比如 /home/1 和 /home/2 都是跳转到 /home/:id 

那么在 router 中这样写: 

    {
      path: '/box/:id',
      name: 'info_id',
      component: InfoID
    }

跳转语句这样写:  this.$router.push({path: '/home/' + res.data.id})

this.$route.params.id 可以获得参数 id 的值。

posted on 2019-04-28 17:21  mlllily  阅读(92)  评论(0)    收藏  举报