vue2.x学习笔记(二十九)

接着前面的内容:https://www.cnblogs.com/yanggb/p/12682822.html

路由

官方路由

对于大多数的单页面应用,都推荐使用官方支持的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,整合起来也一样简单。

"我还是很喜欢你,像春宵红帐梦无息,犹恐念起。"

猜你喜欢

转载自www.cnblogs.com/yanggb/p/12682902.html