class HistoryRoute {
constructor() {
this.current = null
}
}
class VueRouter {
constructor(options) {
this.mode = options.mode || 'hash'
this.routes = options.routes || []
this.routesMap = this.createMap(this.routes)
this.history = new HistoryRoute()
this.init()
}
init() {
if (this.mode === 'hash') {
if (!location.hash) {
location.hash = '/'
}
window.addEventListener('load', () => {
console.log('load')
this.history.current = location.hash.slice(1)
})
window.addEventListener('hashchange', () => {
console.log('hashchange')
this.history.current = location.hash.slice(1)
})
} else {
if (location.hash) {
location.pathname = '/'
}
window.addEventListener('load', () => {
console.log('load')
this.history.current = location.pathname
})
window.addEventListener('popstate', () => {
console.log('popstate')
this.history.current = location.pathname
})
}
}
createMap(routes) {
return routes.reduce((memo, current) => {
memo[current.path] = current.component
return memo
}, {})
}
go() {
}
push(path) {
window.history.pushState({}, '', path)
this.history.current = path
}
back() {
}
afterEach() {
console.log('after')
}
beforeEach() {
}
}
VueRouter.install = function (Vue) {
console.log(Vue, 'install')
Vue.mixin({
beforeCreate() {
if (this.$options && this.$options.router) { // 定位根組件
this._root = this
this._router = this.$options.router
Vue.util.defineReactive(this, 'xxx', this._router.history)
} else { // 子節(jié)點
try {
this._root = this.$parent._root
} catch (e) {
console.log(this, e)
}
}
Object.defineProperty(this, '$router', {
get() {
return this._root._router
}
})
Object.defineProperty(this, '$route', {
get() {
return {
current: this._root._router.history.current
}
}
})
}
})
Vue.component('router-link', {
props: {
to: String,
tag: String
},
render(h) {
let mode = this._self._root._router.mode
let tag = this.tag || 'a'
console.log(this)
return h(tag, {
attrs: {
href: this._self._root._router.mode === 'hash' ? `#${this.to}` : this.to,
},
on: {
click: () => {
this.handleClick()
}
}
}, this.$slots.default)
},
methods: {
handleClick() {
this._root._router.push(this.to)
event.returnValue = false
}
}
})
Vue.component('router-view', {
render(h) {
let current = this._self._root._router.history.current
let routeMap = this._self._root._router.routesMap
console.log(this)
return h(routeMap[current])
}
})
}
export default VueRouter
Vue-router 簡單實現(xiàn)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- key words : vue-cli vue-router webpack 在vue-cli+vue-route...
- 直接貼路由配置文件代碼 說明:在這里我們 router 的 meta屬性 添加了 自定義的breadcrumb對象...
- github地址https://github.com/JohnnyZhangQiao/vue-family 如果對...
- vuex-router-sync 插件通過動態(tài)注冊模塊將 vue-router 和 vuex 結(jié)合在一起,實現(xiàn)應(yīng)...