File tree 4 files changed +14
-2
lines changed
4 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
我们在使用Vue.js开发复杂的应用时,经常会遇到多个组件共享同一个状态,亦或是多个组件会去更新同一个状态,在应用代码量较少的时候,我们可以组件间通信去维护修改数据,或者是通过事件总线来进行数据的传递以及修改。但是当应用逐渐庞大以后,代码就会变得难以维护,从父组件开始通过prop传递多层嵌套的数据由于层级过深而显得异常脆弱,而事件总线也会因为组件的增多、代码量的增大而显得交互错综复杂,难以捋清其中的传递关系。
4
4
5
- 那么为什么我们不能将数据层与组件层抽离开来呢?把数据层放到全局形成一个单一的Store,组件层变得更薄,专门用来进行数据的展示及操作。所有数据的操作都需要经过全局的Store来进行 ,形成一个单向数据流,使数据变化变得“可预测”。
5
+ 那么为什么我们不能将数据层与组件层抽离开来呢?把数据层放到全局形成一个单一的Store,组件层变得更薄,专门用来进行数据的展示及操作。所有数据的变更都需要经过全局的Store来进行 ,形成一个单向数据流,使数据变化变得“可预测”。
6
6
7
7
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 ) 。
8
8
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import { AbstractHistory } from './history/abstract'
15
15
16
16
import type { Matcher } from './create-matcher'
17
17
18
+ /* 导出的VueRouter对象,用来包装store */
18
19
export default class VueRouter {
19
20
static install : ( ) => void ;
20
21
static version : string ;
@@ -34,6 +35,7 @@ export default class VueRouter {
34
35
35
36
constructor ( options : RouterOptions = { } ) {
36
37
this . app = null
38
+ /* 保存vm实例 */
37
39
this . apps = [ ]
38
40
this . options = options
39
41
this . beforeHooks = [ ]
@@ -80,20 +82,25 @@ export default class VueRouter {
80
82
return this . history && this . history . current
81
83
}
82
84
85
+ /* 初始化 */
83
86
init ( app : any /* Vue component instance */ ) {
87
+ /* 未安装就调用init会抛出异常 */
84
88
process . env . NODE_ENV !== 'production' && assert (
85
89
install . installed ,
86
90
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
87
91
`before creating root instance.`
88
92
)
89
93
94
+ /* 将当前vm实例保存在app中 */
90
95
this . apps . push ( app )
91
96
92
97
// main app already initialized.
98
+ /* 已存在说明已经被init过了,直接返回 */
93
99
if ( this . app ) {
94
100
return
95
101
}
96
102
103
+ /* this.app保存当前vm实例 */
97
104
this . app = app
98
105
99
106
const history = this . history
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ export function install (Vue) {
16
16
/* 判断是否已定义 */
17
17
const isDef = v => v !== undefined
18
18
19
- /* 注册router实例 */
19
+ /* 通过registerRouteInstance方法注册router实例 */
20
20
const registerInstance = ( vm , callVal ) => {
21
21
let i = vm . $options . _parentVnode
22
22
if ( isDef ( i ) && isDef ( i = i . data ) && isDef ( i = i . registerRouteInstance ) ) {
@@ -34,11 +34,15 @@ export function install (Vue) {
34
34
this . _routerRoot = this
35
35
/* 保存router */
36
36
this . _router = this . $options . router
37
+ /* VueRouter对象的init方法 */
37
38
this . _router . init ( this )
39
+ /* Vue内部方法,为对象defineProperty上在变化时通知的属性 */
38
40
Vue . util . defineReactive ( this , '_route' , this . _router . history . current )
39
41
} else {
42
+ /* 非根组件则直接从父组件中获取 */
40
43
this . _routerRoot = ( this . $parent && this . $parent . _routerRoot ) || this
41
44
}
45
+ /* 通过registerRouteInstance方法注册router实例 */
42
46
registerInstance ( this , this )
43
47
} ,
44
48
destroyed ( ) {
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ function clone (value) {
49
49
}
50
50
51
51
// the starting route that represents the initial state
52
+ /* 起始路由 */
52
53
export const START = createRoute ( null , {
53
54
path : '/'
54
55
} )
You can’t perform that action at this time.
0 commit comments