当 Vue 实例被创建时,添加所有在 Data 对象中的属性到 Reactivity System 中。
而创建之后添加赋值新属性,则不 Reactive
而实例被创建之时,会自带前缀为 $ 的一系列属性和方法
vm.$data
vm.$props
vm.$el
vm.$options
vm.$parent
vm.$root
vm.$children
vm.$slots
vm.$scopedSlots
vm.$refs
vm.$isServer
vm.$attrs
vm.$listeners
vm.$watch
vm.$set
vm.$delete
vm.$on
vm.$once
vm.$off
vm.$emit
vm.$mount
vm.$forceUpdate
vm.$nextTick
vm.$destroy
VueJS 编译 模板为 VirtualDOM render 函数,如果对 VirtualDom 非常熟悉的话,则可以使用
# text
{{ msg }}
<span v-once> {{ msg }} <span>
# raw html
<div v-html="rawHTML"></div>
# attributes
<div v-bind:id="dynamicID"></div>
# JS Expressions
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
<div v-bind:id="'list-' + id"></div>
<div class="static"
:class="{ active: isActive, 'text-danger': hasError }">
</div>
<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
<my-component class="baz boo"></my-component>
<p class="origin baz boo">
<h1 v-if="ok">Yes</h1>
<h1 v-if="ok">Yes</h1>
<h1 v-else>No</h1>
<div v-if="type === 'A'">
A
</div>
<div v-else-if="type === 'B'">
B
</div>
<div v-else-if="type === 'C'">
C
</div>
<div v-else>
Not A/B/C
</div>
对于数组
<ul id="example-2">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul>
对于对象
<ul id="example-2">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul>
filter
直接通过 index 对数组进行修改 不应该 vm.items[indexOfItem] = newValue 应该 //
Vue.set Vue.set(example1.items, indexOfItem, newValue) // Array.prototype.splice
example1.items.splice(indexOfItem, 1, newValue) 修改 length 不该 vm.items.length
= newLength 应该 example1.items.splice(newLength)
猜测背后可能是因为这种方式是无法监测设置
js 无法直接提供对
this.$set(this.userProfile, 'age', 27)
<div id="example-1">
<button v-on:click="counter += 1">Add 1</button>
<p>The button above has been clicked {{ counter }} times.</p>
</div>
PS: 注意,前面 v-on v-bind 所有的绑定可以绑在方法上,也可以直接填单行表达式。可以提高可阅读性
可以绑定方法
可以绑定方法加参数,配合 for / if
事件修饰符
.stop
.prevent
.capture
.self
.once
<!-- the click event's propagation will be stopped -->
<a v-on:click.stop="doThis"></a>
<!-- the submit event will no longer reload the page -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- modifiers can be chained -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- just the modifier -->
<form v-on:submit.prevent></form>
<!-- use capture mode when adding the event listener -->
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
<div v-on:click.capture="doThis">...</div>
<!-- only trigger handler if event.target is the element itself -->
<!-- i.e. not from a child element -->
<div v-on:click.self="doThat">...</div>
.enter
.tab
.delete (captures both “Delete” and “Backspace” keys)
.esc
.space
.up
.down
.left
.right
<!-- same as above -->
<input v-on:keyup.enter="submit" />
<!-- also works for shorthand -->
<input @keyup.enter="submit" />
键盘
.ctrl
.alt
.shift
.meta
<!-- Alt + C -->
<input @keyup.alt.67="clear">
<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">Do something</div>
鼠标
.left
.right
.middle
在以往的开发中,直接写在 html 的 onclick 是很糟糕的方式,因为这违反了局部变量准则。将 onclick 变量提升为整个页面很容易导致,页面内部组织混乱。
而 v-on 则仅执行 viewmodel 的方法,不会绑定到其他方法里。
https://vuejs.org/v2/guide/forms.html
对于每个 Form 空间,可以进行一定的封装。
比如拖拽上传啦,比如 RadioGroup 啦等等。通过封装,可以进行相关的分析。
Using Components
Global Registration
Local Registration
DOM Template Parsing Caveats
data Must Be a Function
Composing Components
Props
Passing Data with Props
camelCase vs. kebab-case
Dynamic Props
Literal vs. Dynamic
One-Way Data Flow
Prop Validation
Non-Prop Attributes
Replacing/Merging with Existing Attributes
Custom Events
Using v-on with Custom Events
Binding Native Events to Components
.sync Modifier
Form Input Components using Custom Events
Customizing Component v-model
Non Parent-Child Communication
Content Distribution with Slots
Compilation Scope
Single Slot
Named Slots
Scoped Slots
Dynamic Components
keep-alive
Misc
Authoring Reusable Components
Child Component Refs
Async Components
Advanced Async Components
Component Naming Conventions
Recursive Components
Circular References Between Components
Inline Templates
X-Templates
Cheap Static Components with v-once
组件化是 Vue 模块化组织前端网页的方式。
Vue 的组件化,将模板,JavaScript 与样式放在一起。出于代码的复用性:
组件化可以给组件子组件们组织起来,起一个阅读性更好的名称,从而使得编写 Vue 组件更加语义化。
Vue 通过组件来组织代码,但糟糕的是并不是一切都可以被组件化
下面的内容从 Vue 作者的知乎 Live 上取来。
http://hcysun.me/2017/03/03/Vue 源码学习 /
组件为基本单元
页面 ->应用(模块,组件树(偏展示))
<router-view>
,<transition>
,作为一种扩展、抽象机制存在。view = render(state)
push pull
vue 混合式
ChangeLog: