Skip to content

Commit cb62dec

Browse files
committed
completed
1 parent c860bb7 commit cb62dec

File tree

12 files changed

+434
-24
lines changed

12 files changed

+434
-24
lines changed

package-lock.json

Lines changed: 83 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
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",
15+
"vuex": "^3.4.0"
1416
},
1517
"devDependencies": {
1618
"@vue/cli-plugin-babel": "^4.1.0",
1719
"@vue/cli-plugin-eslint": "^4.1.0",
20+
"@vue/cli-plugin-router": "^4.5.13",
21+
"@vue/cli-plugin-vuex": "^4.5.13",
1822
"@vue/cli-service": "^4.1.0",
1923
"babel-eslint": "^10.0.3",
2024
"eslint": "^5.16.0",

src/App.vue

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
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+
<p @click="$store.commit('add')">{{ $store.state.counter }}</p>
8+
<p @click="$store.dispatch('add')">async{{ $store.state.counter }}</p>
9+
<p>double: {{$store.getters.doubleCounter}}</p>
10+
<!-- 占位符 -->
11+
<router-view />
512
</div>
613
</template>
714

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-
1915
<style>
2016
#app {
21-
font-family: 'Avenir', Helvetica, Arial, sans-serif;
17+
font-family: "Avenir", Helvetica, Arial, sans-serif;
2218
-webkit-font-smoothing: antialiased;
2319
-moz-osx-font-smoothing: grayscale;
2420
text-align: center;
2521
color: #2c3e50;
26-
margin-top: 60px;
22+
}
23+
24+
#nav {
25+
padding: 30px;
26+
}
27+
28+
#nav a {
29+
font-weight: bold;
30+
color: #2c3e50;
31+
}
32+
33+
#nav a.router-link-exact-active {
34+
color: #42b983;
2735
}
2836
</style>

src/krouter/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Vue from 'vue'
2+
import VueRouter from './kvue-router'
3+
import Home from '../views/Home.vue'
4+
5+
// 引入插件
6+
// use方法将来会调用install方法
7+
Vue.use(VueRouter)
8+
9+
// 路由映射表
10+
const routes = [
11+
{
12+
path: '/',
13+
name: 'Home',
14+
component: Home,
15+
},
16+
{
17+
path: '/about',
18+
name: 'About',
19+
// route level code-splitting
20+
// this generates a separate chunk (about.[hash].js) for this route
21+
// which is lazy-loaded when the route is visited.
22+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
23+
}
24+
]
25+
26+
const router = new VueRouter({
27+
routes
28+
})
29+
30+
export default router

src/krouter/kvue-router.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
let Vue;
2+
3+
// vue插件编写
4+
// 实现一个install方法
5+
class VueRouter {
6+
constructor(options) {
7+
console.log(Vue);
8+
this.$options = options;
9+
10+
// 保存当前hash到current
11+
// current应该是响应式的
12+
// 给指定对象定义响应式属性
13+
Vue.util.defineReactive(
14+
this,
15+
"current",
16+
window.location.hash.slice(1) || "/"
17+
);
18+
// this.current = "/";
19+
20+
// 监控hashchange
21+
window.addEventListener("hashchange", () => {
22+
// #/about => /about
23+
this.current = window.location.hash.slice(1);
24+
});
25+
}
26+
}
27+
28+
// 形参1是Vue构造函数: 目的是便于扩展
29+
VueRouter.install = function(_Vue) {
30+
Vue = _Vue;
31+
32+
// 1. 将$router注册一下
33+
// 下面代码延迟未来某个时刻:根实例创建时
34+
Vue.mixin({
35+
beforeCreate() {
36+
// 只需要根实例时执行一次
37+
if (this.$options.router) {
38+
// 希望将来任何组件都可以通过$router
39+
// 访问路由器实例
40+
Vue.prototype.$router = this.$options.router;
41+
}
42+
},
43+
});
44+
45+
// 2. 注册两个全局组件:router-Link, router-view
46+
Vue.component("router-link", {
47+
// template: '<a>router-link</a>'
48+
props: {
49+
to: {
50+
type: String,
51+
required: true,
52+
},
53+
},
54+
render(h) {
55+
// h就是createElement()
56+
// 作用:返回一个虚拟dom
57+
// <router-link to="/about">abc</router-link>
58+
// return <a href={"#" + this.to}>{this.$slots.default}</a>;
59+
// 获取插槽内容:this.$slots.default
60+
return h(
61+
"a",
62+
{
63+
attrs: {
64+
href: "#" + this.to,
65+
},
66+
},
67+
this.$slots.default
68+
);
69+
},
70+
});
71+
72+
Vue.component("router-view", {
73+
// vue.runtime.js
74+
// vue.js compiler -> template -> render()
75+
// template: '<div>router-view</div>'
76+
render(h) {
77+
// 可以传入一个组件直接渲染
78+
// 思路:如果可以根据url的hash部分动态匹配这个要渲染的组件
79+
// window.location.hash
80+
// console.log(this.$router.$options.routes);
81+
// console.log(this.$router.current);
82+
let component = null;
83+
const route = this.$router.$options.routes.find(
84+
(route) => route.path === this.$router.current
85+
);
86+
if (route) {
87+
component = route.component
88+
}
89+
return h(component);
90+
},
91+
});
92+
};
93+
94+
export default VueRouter;

src/kstore/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Vue from 'vue'
2+
import Vuex from './kvuex'
3+
4+
Vue.use(Vuex)
5+
6+
export default new Vuex.Store({
7+
// state应该是响应式对象
8+
state: {
9+
counter: 0
10+
},
11+
mutations: {
12+
// state从何而来
13+
add(state) {
14+
state.counter++
15+
}
16+
},
17+
actions: {
18+
// 上下文从何而来,长什么样
19+
add({commit}) {
20+
setTimeout(() => {
21+
commit('add')
22+
}, 1000);
23+
}
24+
},
25+
getters: {
26+
doubleCounter: state => {
27+
return state.counter * 2;
28+
}
29+
}
30+
})

0 commit comments

Comments
 (0)