omi/packages/omi-page
张磊 73b9368817 update readme 2018-10-26 11:25:07 +08:00
..
examples/simple update readme 2018-10-26 11:19:25 +08:00
README.md update readme 2018-10-26 11:25:07 +08:00
index.js add omi-page 2018-10-26 10:50:12 +08:00
package.json add omi-page 2018-10-26 10:50:12 +08:00
webpack.config.js add omi-page 2018-10-26 10:50:12 +08:00

README.md

omi-page

Tiny client-side router by page.

→ demo

Usage

import { render, tag, WeElement } from 'omi'
import page from 'omi-page'

@tag('my-app')
class MyApp extends WeElement {
    installed() {
        page('/', this.index)
        page('/about', this.about)
        page('/contact', this.contact)
        page('/contact/:contactName', this.contact)
        page('*', this.notfound)
        page()
    }

    render() {
        return (
            <div>
                <ul>
                    <li><a href="/">/</a></li>
                    <li><a href="/about">/about</a></li>
                    <li><a href="/contact">/contact</a></li>
                    <li><a href="/contact/me">/contact/me</a></li>
                    <li><a href="/contact/me?a=b">/contact/me?a=b</a></li>
                    <li><a href="/not-found?foo=bar">/not-found</a></li>
                </ul>
                <p>
                    {this.data.path}
                </p>
            </div>
        )
    }

    index = (ctx) => {
        this.data.path = ctx.path
        this.update()
    }

    about = (ctx) => {
        this.data.path = ctx.path
        this.update()
    }

    contact = (ctx) => {
        this.data.path = ctx.path
        this.update()
    }

    notfound = (ctx) => {
        this.data.path = ctx.path
        this.update()
    }
}

render(<my-app></my-app>, 'body')

page.base([path])

Get or set the base path. For example if page.js is operating within /blog/* set the base path to "/blog"

 page.base('/blog')

License

MIT © dntzhang