React、Vue学习总结

2019-04-05  本文已影响0人  粑粑八成

一、 从MVC到MVVM

从MVC到MVVM

1. (客户端)MVC调用关系

用户通过操作view调用controller,controller决定调用哪个modal,最后modal使用观察者模式触发view的监听事件

2. MVVM模式

总结: ViewModa是面向界面的数据,其中包含双向绑定的引擎
1). Model-View-ViewModel,除了领域模型(Domain Model)和视图(View)之外增加了ViewModal用于同步Modal和View
2). ViewModel的含义就是 "Model of View",视图的模型。 在图形界面应用程序当中,界面所提供的信息可能不仅仅包含应用程序的领域模型。还可能包含一些领域模型不包含的视图状态。不在领域模型之中但是描述界面所必须的数据,如排序、隐藏显示等状态(State)
3). ViewModal中会有一个Binder或者Data-binding engine的东西,View和Modal中的数据同步问题由Binder处理。你只需要在View的模板语法中,指令式的声明View上显示的内容是与哪一块数据绑定的。
4). 当ViewModel对进行Model更新的时候,Binder会自动把数据更新到View上去,当用户对View进行操作(例如表单输入),Binder也会自动把数据更新到Model上去。这种方式称为:Two-way data-binding,双向数据绑定。

二、对比

1. 数据渲染和绑定

1). View(HTML)

2). ViewModel

3). 发起异步请求

4). props

props: ['initialCounter'],
data: function () {
  return {
    counter: this.initialCounter
  }
}

props: ['size'],
computed: {
  normalizedSize: function () {
    return this.size.trim().toLowerCase()
  }
}

5). React绑定this

6). JSX语法绑定监听函数

<Button onClick={() => this.showModal('create')} />//  定义新的函数
<Button onClick={this.handleDelete} /> // 函数引用,会接受一个event
// 这样写会死循环,{}里的是js语句,在渲染时就会执行,而不是点击的时候
<Button onClick={this.handleDelete()} />
<div id="example-2">
  <!-- `greet` 是在下面定义的方法名 -->
  <button v-on:click="greet">Greet</button>
</div>

<div id="example-3">
  <button v-on:click="say('hi')">Say hi</button>
  <button v-on:click="say('what')">Say what</button>
</div>

<button v-on:click="warn('Form cannot be submitted yet.', $event)">
  Submit
</button>

7). 嵌套组件和插槽

8). vuex和redux代替方案

9). vue有更多的API
- vue自定义事件
- vue nextTick

2. 路由

1). 路由配置

2). 授权路由

3. 状态管理

1). 获取store上的数据

4. 虚DOM和Diff算法

上一篇 下一篇

猜你喜欢

热点阅读