Skip to content

Commit 3b07c07

Browse files
committed
update vue-router annotations
1 parent a67db4f commit 3b07c07

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

docs/Vuex源码解析.MarkDown

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
我们在使用Vue.js开发复杂的应用时,经常会遇到多个组件共享同一个状态,亦或是多个组件会去更新同一个状态,在应用代码量较少的时候,我们可以组件间通信去维护修改数据,或者是通过事件总线来进行数据的传递以及修改。但是当应用逐渐庞大以后,代码就会变得难以维护,从父组件开始通过prop传递多层嵌套的数据由于层级过深而显得异常脆弱,而事件总线也会因为组件的增多、代码量的增大而显得交互错综复杂,难以捋清其中的传递关系。
44

5-
那么为什么我们不能将数据层与组件层抽离开来呢?把数据层放到全局形成一个单一的Store,组件层变得更薄,专门用来进行数据的展示及操作。所有数据的操作都需要经过全局的Store来进行,形成一个单向数据流,使数据变化变得“可预测”。
5+
那么为什么我们不能将数据层与组件层抽离开来呢?把数据层放到全局形成一个单一的Store,组件层变得更薄,专门用来进行数据的展示及操作。所有数据的变更都需要经过全局的Store来进行,形成一个单向数据流,使数据变化变得“可预测”。
66

77
Vuex是一个专门为Vue.js框架设计的、用于对Vue.js应用程序进行状态管理的库,它借鉴了Flux、redux的基本思想,将共享的数据抽离到全局,以一个单例存放,同时利用Vue.js的响应式机制来进行高效的状态管理与更新。正是因为Vuex使用了Vue.js内部的“响应式机制”,所以Vuex是一个专门为Vue.js设计并与之高度契合的框架(优点是更加简洁高效,缺点是只能跟Vue.js搭配使用)。具体使用方法及API可以参考[Vuex的官网](https://vuex.vuejs.org/zh-cn/intro.html)
88

vue-router-src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AbstractHistory } from './history/abstract'
1515

1616
import type { Matcher } from './create-matcher'
1717

18+
/* 导出的VueRouter对象,用来包装store */
1819
export default class VueRouter {
1920
static install: () => void;
2021
static version: string;
@@ -34,6 +35,7 @@ export default class VueRouter {
3435

3536
constructor (options: RouterOptions = {}) {
3637
this.app = null
38+
/* 保存vm实例 */
3739
this.apps = []
3840
this.options = options
3941
this.beforeHooks = []
@@ -80,20 +82,25 @@ export default class VueRouter {
8082
return this.history && this.history.current
8183
}
8284

85+
/* 初始化 */
8386
init (app: any /* Vue component instance */) {
87+
/* 未安装就调用init会抛出异常 */
8488
process.env.NODE_ENV !== 'production' && assert(
8589
install.installed,
8690
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
8791
`before creating root instance.`
8892
)
8993

94+
/* 将当前vm实例保存在app中 */
9095
this.apps.push(app)
9196

9297
// main app already initialized.
98+
/* 已存在说明已经被init过了,直接返回 */
9399
if (this.app) {
94100
return
95101
}
96102

103+
/* this.app保存当前vm实例 */
97104
this.app = app
98105

99106
const history = this.history

vue-router-src/install.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function install (Vue) {
1616
/* 判断是否已定义 */
1717
const isDef = v => v !== undefined
1818

19-
/* 注册router实例 */
19+
/* 通过registerRouteInstance方法注册router实例 */
2020
const registerInstance = (vm, callVal) => {
2121
let i = vm.$options._parentVnode
2222
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
@@ -34,11 +34,15 @@ export function install (Vue) {
3434
this._routerRoot = this
3535
/* 保存router */
3636
this._router = this.$options.router
37+
/* VueRouter对象的init方法 */
3738
this._router.init(this)
39+
/* Vue内部方法,为对象defineProperty上在变化时通知的属性 */
3840
Vue.util.defineReactive(this, '_route', this._router.history.current)
3941
} else {
42+
/* 非根组件则直接从父组件中获取 */
4043
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
4144
}
45+
/* 通过registerRouteInstance方法注册router实例 */
4246
registerInstance(this, this)
4347
},
4448
destroyed () {

vue-router-src/util/route.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function clone (value) {
4949
}
5050

5151
// the starting route that represents the initial state
52+
/* 起始路由 */
5253
export const START = createRoute(null, {
5354
path: '/'
5455
})

0 commit comments

Comments
 (0)