对于大多数单页面应用,都推荐使用官方支持的vue-router库。更多细节可以看vue-router文档。

如果只需要非常简单的路由而不需要引入整个路由库,可以动态渲染一个页面级的组件像这样:
const NotFound = { template: 'Page not found
' }
const Home = { template: 'home page
' }
const About = { template: 'about page
' }
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 History API,你可以建立一个非常基本但功能齐全的客户端路由器。可以直接看实例应用。
如果有非常喜欢的第三方路由,如Page.js或者 Director, 整合很简单。 这有个用了Page.js的复杂示例。
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号