Git Product home page Git Product logo

marko-router5's Introduction

Getting Started

Flexible and powerful routing for MarkoJS.

Full documentation: https://jesse1983.github.io/marko-router5

Installation

Install marko-router5 package:

npm i marko-router5 -D

Usage

Define routes on main marko file:

<!-- Define routes -->
$ const routes = [
  { name: 'home', path: '/', component: require('./home.marko') },
  { name: 'products', path: '/products', component: require('./products.marko') },
  { name: 'contact', path: '/contact', component: require('./contact.marko') },
]

<div>
  <router routes=routes />
</div>

Navigate

Add route-link tag instead a tag:

<route-link href="/" class="nav-item">Home</route-link>
<route-link href="/products" class="nav-item">Products</route-link>
<route-link href="/contact" class="nav-item">Contact</route-link>

or

Import navigate method:

const { navigate } = require('marko-router5');

navigate('/products');

Nested routes

$ const routes = [
  { name: 'products', path: '/products', component: require('./products.marko'), children: [
    { name: 'notebooks', path: '/notebooks', component: require('./notebooks.marko') },
    { name: 'desktops', path: '/desktops', component: require('./desktops.marko') },
    { name: 'others', path: '/others', component: require('./others.marko'), children: [
      { name: 'cpu-processors', path: '/cpu-processors', component: require('./cpu-processors.marko') },
      { name: 'memory-cards', path: '/memory-cards', component: require('./memory-cards.marko') },
    ] },
  ] },
]

Advanced

Router

Attributes

Options

All router options are on Router Options page.

<router routes=routes options={ defaultRoute: 'home' } />

InitialPath

A initial path can be a string (ex: /dashboard) or a bool (if true, initial path will be current window location).

<router routes=routes initial-path="/dashboard" />
<!-- or -->
<router routes=routes initial-path />

noWrapper

Remove <div class="router5-placeholder"> wrapper.

<router routes=routes no-wrapper />

Events

transitionStart

Before route change. Value: an object with next and previous route state { toState, fromState }:

class {
  transitionStartMethod(states) {
    // use states.toState or states.fromState
  }
}
<route routes=routes on-transition-start('transitionStartMethod') />

transitionSuccess

After route change. Value: an object with next and previous route state { toState, fromState }:

class {
  transitionSuccessMethod(states) {
    // use states.toState or states.fromState
  }
}
<route routes=routes on-transition-success('transitionSuccessMethod') />

transitionError

After route change and throw an error. Value: an object with next and previous route state and the error { toState, fromState, err }:

class {
  transitionErrorMethod(states) {
    // use states.toState, states.fromState or states.err
  }
}
<route routes=routes on-transition-error('transitionErrorMethod') />

Route Link

Attributes

ActiveClass

Automatically current link will have class active. You can change class name using attribute active-class:

<route-link href="/" class="nav-item" active-class="current">Home</route-link>

Result is:

<a href="/" class="nav-item current">Home</a>

ParentClass

If you need add active class on parent element instead a tag, add parent-class:

<li>
  <route-link href="/" class="nav-item" parent-class>Home</route-link>
</li>

Result is:

<li class="active">
  <a href="/" class="nav-item">Home</a>
</li>

marko-router5's People

Contributors

jesse1983 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.