官方路由
對於大多數單頁面應用,推薦使用官方支持的vue-router library,對於更多的細節,查看vue-router的文檔。
從零開始簡單的路由
如果你僅僅需要一個非常簡單的路由,但不需要包含一個完整功能的路由庫,你可以像這樣動態渲染頁面級別的組件:
const NotFound = { template: '<p>Page Not found</p>' }
const Home = { template: '<p>home page</p>'}
const About = { template: '<p>about page</p>' }
const routes = {
'/': Home,
'/about': About
}
new Vue({
el: '#app',
data: {
currentRoute: window,location.pathname
},
computed: {
ViewComponent () {
return routes[this.currentRoute] || NotFound
}
},
render(h) { return h(this.ViewComponent) }
})
結合HTML5歷史API,你能夠構建一個非常基礎但功能全面的客戶端路由。可以直接看這個例子。
集成第三方路由
如果你傾向於使用第三方路由,例如Page.js or Director, 集成同樣簡單。這裏有一個使用Page.js的完整例子。