天天看點

Vue簡單建立一個項目(十)

程式設計式導航就是用JavaScript來跳轉頁面

<button @click="gotoNews()"></button>
methods:{
gotoNews(){
this.$router.push({path:'/details/111'})
}
}
           

命名式路由跳轉

前提條件是路由改寫命名規則:

{ path: ‘/news’, component: News,name:‘news’ },
<button @click="gotoNews()"></button>
 methods:{
    gotoNews(){
       router.push({ name: 'news', params: { userId: 123 }})
       this.$router.push({ name: 'news'})
    }
    }
           

hash模式就是指的url位址上面出現的#,這種規則會讓人覺的莫名其妙,不知所措,不讓人明白你的意圖。

http://localhost:8080/#/details/494

變為普通模式的方式就是:

const router = new VueRouter({
  mode: 'history',   /*hash模式改為history*/※※※※※※※※※※※※※※※
  routes // (縮寫)相當于 routes: routes
})
           
http://localhost:8080/details/494