2018-10-26 10:52:51 +08:00
|
|
|
|
# omi-page
|
|
|
|
|
|
|
|
|
|
Tiny client-side router by [page](https://github.com/visionmedia/page.js).
|
|
|
|
|
|
2018-10-26 11:17:51 +08:00
|
|
|
|
[→ demo](https://tencent.github.io/omi/packages/omi-page/examples/simple/)
|
|
|
|
|
|
2018-10-26 10:52:51 +08:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
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')
|
|
|
|
|
```
|
|
|
|
|
|
2018-10-26 11:25:07 +08:00
|
|
|
|
## page.base([path])
|
|
|
|
|
|
|
|
|
|
Get or set the base path. For example if page.js is operating within /blog/* set the base path to "/blog":
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
page.base('/blog')
|
|
|
|
|
```
|
|
|
|
|
|
2018-10-26 10:52:51 +08:00
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT © dntzhang
|