//在router文件中的index.js中引入tvue-router.js
import Vue from 'vue'
// import VueRouter from 'vue-router'
import VueRouter from './tvue-router'
import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
let Vue;
class VueRouter {
constructor(options){
this.$options = options
const initial = window.location.hash.slice(1)||'/'
//将hash值变为响应式的值,当其改变时,用到该值得组件会刷新
Vue.util.defineReactive(this,'current',initial)
window.addEventListener('hashchange',()=>{
this.current = window.location.hash.slice(1)
})
}
}
VueRouter.install = function(_Vue){
Vue = _Vue
Vue.mixin ({
beforeCreate(){
if(this.$options.router){
Vue.prototype.$router = this.$options.router
}
}
});
Vue.component('router-link',{
props:{
to:{
type:String,
default:'/'
}
},
render(h){
console.log(this.$slots)
return h('a',{
attrs:{
href:"#"+this.to
}
},
this.$slots.default
)
}
})
Vue.component('router-view',{
render(h){
console.log(this.$router)
let component = null;
const route = this.$router.$options.routes.find(route=>route.path===this.$router.current)
if(route){
component = route.component
}
return h(component)
}
})
}
export default VueRouter
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)