Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1007 Bytes

routing.md

File metadata and controls

39 lines (31 loc) · 1007 Bytes
title type order
Routing
guide
501

Official Router

For most Single Page Applications, it's recommended to use the officially-supported kdu-router library. For more details, see kdu-router's documentation.

Simple Routing From Scratch

If you only need very simple routing and do not wish to involve a full-featured router library, you can do so by dynamically rendering a page-level component like this:

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 Kdu({
  el: '#app',
  data: {
    currentRoute: window.location.pathname
  },
  computed: {
    ViewComponent () {
      return routes[this.currentRoute] || NotFound
    }
  },
  render (h) { return h(this.ViewComponent) }
})

Combined with the HTML5 History API, you can build a very basic but fully-functional client-side router.