官方路由
对于大多数单页面应用,都推荐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:{
ViewComputed(){
return routes[this.currentPage] || NotFound
}
},
render(h){
return h(this.ViewComponent)
}
})
结合HTML5History API,你可以建立一个非常基础但功能齐全的客户端路由器。
整合第三方路由。