Skip to content

Commit 09dc02c

Browse files
committed
kvue-router
1 parent c860bb7 commit 09dc02c

File tree

10 files changed

+280
-20
lines changed

10 files changed

+280
-20
lines changed

package-lock.json

+75-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"dependencies": {
1111
"core-js": "^3.4.3",
1212
"element-ui": "^2.4.5",
13-
"vue": "^2.6.10"
13+
"vue": "^2.6.10",
14+
"vue-router": "^3.2.0"
1415
},
1516
"devDependencies": {
1617
"@vue/cli-plugin-babel": "^4.1.0",
1718
"@vue/cli-plugin-eslint": "^4.1.0",
19+
"@vue/cli-plugin-router": "^4.5.13",
1820
"@vue/cli-service": "^4.1.0",
1921
"babel-eslint": "^10.0.3",
2022
"eslint": "^5.16.0",

src/App.vue

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
<template>
22
<div id="app">
3-
<img src="./assets/logo.png">
4-
<HelloWorld msg="Welcome to Your Vue.js App"/>
3+
<div id="nav">
4+
<router-link to="/">Home</router-link> |
5+
<router-link to="/about">About</router-link>
6+
</div>
7+
<!-- 路由出口 -->
8+
<router-view/>
59
</div>
610
</template>
711

8-
<script>
9-
import HelloWorld from './components/HelloWorld.vue'
10-
11-
export default {
12-
name: 'app',
13-
components: {
14-
HelloWorld
15-
}
16-
}
17-
</script>
18-
1912
<style>
2013
#app {
2114
font-family: 'Avenir', Helvetica, Arial, sans-serif;
2215
-webkit-font-smoothing: antialiased;
2316
-moz-osx-font-smoothing: grayscale;
2417
text-align: center;
2518
color: #2c3e50;
26-
margin-top: 60px;
19+
}
20+
21+
#nav {
22+
padding: 30px;
23+
}
24+
25+
#nav a {
26+
font-weight: bold;
27+
color: #2c3e50;
28+
}
29+
30+
#nav a.router-link-exact-active {
31+
color: #42b983;
2732
}
2833
</style>

src/components/HelloWorld.vue

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default {
2929
SlotExample,
3030
TreeExample
3131
},
32+
mounted () {
33+
this.$router
34+
},
3235
}
3336
</script>
3437

src/krouter/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Vue from 'vue'
2+
import VueRouter from './kvue-router'
3+
import Home from '../views/Home.vue'
4+
5+
// 1.VueRouter是一个vue插件
6+
// use调用的时候回执行内部install方法
7+
Vue.use(VueRouter)
8+
9+
const routes = [
10+
{
11+
path: '/',
12+
name: 'Home',
13+
component: Home
14+
},
15+
{
16+
path: '/about',
17+
name: 'About',
18+
// route level code-splitting
19+
// this generates a separate chunk (about.[hash].js) for this route
20+
// which is lazy-loaded when the route is visited.
21+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
22+
}
23+
]
24+
25+
const router = new VueRouter({
26+
routes
27+
})
28+
29+
export default router

src/krouter/kvue-router.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// 我们的自己的插件
2+
// 实现⼀个插件
3+
// 实现VueRouter类
4+
// 处理路由选项
5+
// 监控url变化,hashchange
6+
// 响应这个变化
7+
// 实现install⽅法
8+
9+
let Vue;
10+
11+
// new VueRouter({routes})
12+
class KVueRouter {
13+
constructor(options) {
14+
this.$options = options;
15+
16+
// current必须是响应式的
17+
// 如何做到?
18+
// set只能在响应式对象上动态添加新属性
19+
// new Vue({data() {
20+
// return {
21+
// key: value
22+
// }
23+
// },})
24+
Vue.util.defineReactive(
25+
this,
26+
"current",
27+
window.location.hash.slice(1) || "/"
28+
);
29+
// this.current = window.location.hash.slice(1) || '/'
30+
// 监控hashchange
31+
window.addEventListener("hashchange", () => {
32+
console.log(this.current);
33+
this.current = window.location.hash.slice(1);
34+
});
35+
}
36+
}
37+
38+
// vue插件:需要实现一个静态方法install
39+
// install(Vue, ...)
40+
KVueRouter.install = function(_Vue) {
41+
Vue = _Vue;
42+
43+
// console.log(this);
44+
// $router注册
45+
// 延迟执行注册代码
46+
// 混入:Vue.mixin({beforeCreate(){}})
47+
Vue.mixin({
48+
beforeCreate() {
49+
// console.log(this); // this是组件实例
50+
// 如果当前this是根组件,它选项中必有一个router
51+
if (this.$options.router) {
52+
Vue.prototype.$router = this.$options.router;
53+
}
54+
},
55+
});
56+
57+
// 两个全局组件: router-link、router-view
58+
Vue.component("router-link", {
59+
props: {
60+
to: {
61+
type: String,
62+
required: true,
63+
},
64+
},
65+
render(h) {
66+
// <router-link to="/about">xxx</router-link>
67+
// <a href="#/about">xxx</a>
68+
// return <a href={"#" + this.to}>{this.$slots.default}</a>
69+
return h("a", { attrs: { href: "#" + this.to } }, this.$slots.default);
70+
},
71+
});
72+
Vue.component("router-view", {
73+
// render什么时候会执行?
74+
// init执行一次
75+
// 未来响应式数据变化会再次执行
76+
render(h) {
77+
// 1.获取hash部分#/about
78+
// this.$router.$options.routes
79+
// 2.根据上面地址获取对应组件配置About
80+
// 3.h(About)
81+
console.log(this.$router.$options);
82+
console.log(this.$router.current);
83+
let component = null;
84+
const route = this.$router.$options.routes.find(
85+
(route) => route.path === this.$router.current
86+
);
87+
if (route) {
88+
component = route.component;
89+
}
90+
return h(component);
91+
},
92+
});
93+
};
94+
95+
export default KVueRouter;

src/main.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
import './plugins/element.js'
4+
// import router from './router'
5+
import router from './krouter'
46

57
Vue.config.productionTip = false
68
// 事件总线
79
Vue.prototype.$bus = new Vue()
810

911
new Vue({
10-
render: h => h(App),
12+
router,
13+
render: h => h(App)
1114
}).$mount('#app')

src/router/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
import Home from '../views/Home.vue'
4+
5+
// 1.VueRouter是一个vue插件
6+
// use调用的时候回执行内部install方法
7+
Vue.use(VueRouter)
8+
9+
const routes = [
10+
{
11+
path: '/',
12+
name: 'Home',
13+
component: Home
14+
},
15+
{
16+
path: '/about',
17+
name: 'About',
18+
// route level code-splitting
19+
// this generates a separate chunk (about.[hash].js) for this route
20+
// which is lazy-loaded when the route is visited.
21+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
22+
}
23+
]
24+
25+
const router = new VueRouter({
26+
routes
27+
})
28+
29+
export default router

src/views/About.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div class="about">
3+
<h1>This is an about page</h1>
4+
</div>
5+
</template>

src/views/Home.vue

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="home">
3+
<img alt="Vue logo" src="../assets/logo.png">
4+
<HelloWorld msg="Welcome to Your Vue.js App"/>
5+
</div>
6+
</template>
7+
8+
<script>
9+
// @ is an alias to /src
10+
import HelloWorld from '@/components/HelloWorld.vue'
11+
12+
export default {
13+
name: 'Home',
14+
components: {
15+
HelloWorld
16+
}
17+
}
18+
</script>

0 commit comments

Comments
 (0)