美文网首页vue复习笔记
VUE复习笔记27(路由)

VUE复习笔记27(路由)

作者: XKolento | 来源:发表于2018-09-11 13:04 被阅读10次

官方路由

对于大多数的单页面应用,都推荐使用官方的 vue-router,更多细节可以看 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 History API,你可以建立一个非常基本但功能齐全的客户端路由器。可以直接看实例应用

整合第三方的路由

如果有非常喜欢的第三方路由,如 Page.js 或者 Director,整合很简单。这有个用了 Page.js 的复杂示例

相关文章

网友评论

    本文标题:VUE复习笔记27(路由)

    本文链接:https://www.haomeiwen.com/subject/ihtugftx.html