better <Router> API
This commit is contained in:
parent
c6540be420
commit
c30ec39bd6
2 changed files with 9 additions and 13 deletions
|
@ -12,7 +12,7 @@
|
|||
|
||||
function update() {
|
||||
let {
|
||||
r: { match, pathname, router },
|
||||
r: { match, pathname, router } = {},
|
||||
path,
|
||||
exact,
|
||||
component,
|
||||
|
|
|
@ -1,20 +1,18 @@
|
|||
<div ref:mount/>
|
||||
{{yield}}
|
||||
|
||||
<script>
|
||||
export default {
|
||||
oncreate() {
|
||||
document.addEventListener('click', clickHandler.bind(this))
|
||||
window.addEventListener('popstate', popHandler.bind(this))
|
||||
this._r = new (this.get('component'))({ target: this.refs.mount, data: { r: { router: this, pathname: document.location.pathname } } })
|
||||
this.set({ r: { router: this, pathname: document.location.pathname } })
|
||||
},
|
||||
methods: {
|
||||
go(url, replace) {
|
||||
history[replace ? 'replaceState' : 'pushState'](null, '', location.origin + url)
|
||||
if (this._r) {
|
||||
let r = this._r.get('r')
|
||||
r.pathname = url
|
||||
this._r.set({ r })
|
||||
}
|
||||
let r = this.get('r')
|
||||
r.pathname = url
|
||||
this.set({ r })
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -42,10 +40,8 @@
|
|||
}
|
||||
|
||||
function popHandler() {
|
||||
if (this._r) {
|
||||
let r = this._r.get('r')
|
||||
r.pathname = document.location.pathname
|
||||
this._r.set({ r })
|
||||
}
|
||||
let r = this.get('r')
|
||||
r.pathname = document.location.pathname
|
||||
this.set({ r })
|
||||
}
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue