編程式的導航

在Vue中除了router-link,使用a標籤的形式來實現跳轉,還有一種方式使用window.location.href的形式,叫作編程式導航,就是寫js代碼跳轉。編程

  一:router.push():this

    這個方法,和點擊<router-link :to="...">是同樣的router

    1.最簡單的:this.$router.push(  '/home/newsinfo'+id  )------>字符串路徑對象

    2.傳遞對象的:this.$router.push(  {  path:'/home/newsinfo'+id  }  )路由

    3.傳遞命名路由:this.$router.push(  {name: 'NewsInfo' , params:{  id:  id }})字符串

    4.帶查詢參數,變成   /newsinfo ?plan=privateio

      this.$router.push(  { path: ' newsinfo ', query :{ plan: 'private'} })route

  二:router.replace():方法

    它和router.push很像,惟一的不一樣就是,它不會向history添加新記錄,而是跟它的方法名同樣----替換掉當前的history記錄。命名

    這個方法和<router-link :to="..."  replace>是同樣的; 

相關文章
相關標籤/搜索