Route Resolver

by osban

Level: beginner • Mithril.js Version: latest

This example shows how to use Mithril.js' own router.

Live Example

Dependencies

Type Name URL
scriptmithril@latesthttps://unpkg.com/mithril@latest

JavaScript

const about = {
  view: () =>
    m('div',
      m('h1', "About"),
      m('a', {onclick: () => m.route.set('/')}, "CLICK")
    )
}

const home = {
  view: () =>
    m('div',
      m('h1', "Home"),
      m('a', {onclick: () => m.route.set('/about')}, "CLICK")
    )
}

m.route(document.body, '/', {
  '/'     : home,
  '/about': about
})

CSS

a:hover {
  cursor: pointer;
  color: green
}

As a prerequisite for this snippet, the latest version of Mithril.js framework is required. Beginners should have no problems following this example, that simply shows some basic recipies.

In this code sample Mithril.js' m.route API method is use, besides the basic hyperscript function m().

The example was written by osban, last edits were made on 24 September 2020. The author has contributed some more snippets. Click here to see them all.

Contribute

Did you note a typo or something else? So let me know by opening an issue. Or much better: just fork the repository on GitHub, push your commits and send a pull request. Ready to start your work? Then click on the edit link below. Thanks in advance!

See more code examples  •  Edit this example on GitHub