官方路由
对于大多数单页面应用,推荐使用官方支持的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的完整例子。