|
|
@@ -1,13 +1,12 @@
|
|
|
/* @flow */
|
|
|
|
|
|
-import type Vue from './index'
|
|
|
import type VNode from '../vdom/vnode'
|
|
|
import Watcher from '../observer/watcher'
|
|
|
import { warn, validateProp, remove } from '../util/index'
|
|
|
import { observerState } from '../observer/index'
|
|
|
import { updateListeners } from '../vdom/helpers'
|
|
|
|
|
|
-export function initLifecycle (vm: Vue) {
|
|
|
+export function initLifecycle (vm: Component) {
|
|
|
const options = vm.$options
|
|
|
|
|
|
vm.$parent = options.parent
|
|
|
@@ -24,56 +23,58 @@ export function initLifecycle (vm: Vue) {
|
|
|
vm._isBeingDestroyed = false
|
|
|
}
|
|
|
|
|
|
-export function lifecycleMixin (Vue: Class<Vue>) {
|
|
|
- Vue.prototype._mount = function (): Vue {
|
|
|
- if (!this.$options.render) {
|
|
|
- this.$options.render = () => this.$createElement('div')
|
|
|
+export function lifecycleMixin (Vue: Class<Component>) {
|
|
|
+ Vue.prototype._mount = function (): Component {
|
|
|
+ const vm: Component = this
|
|
|
+ if (!vm.$options.render) {
|
|
|
+ vm.$options.render = () => vm.$createElement('div')
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
- if (this.$options.template) {
|
|
|
+ if (vm.$options.template) {
|
|
|
warn(
|
|
|
'You are using the runtime-only build of Vue where the template ' +
|
|
|
'option is not available. Either pre-compile the templates into ' +
|
|
|
'render functions, or use the compiler-included build.',
|
|
|
- this
|
|
|
+ vm
|
|
|
)
|
|
|
} else {
|
|
|
warn(
|
|
|
'Failed to mount component: template or render function not defined.',
|
|
|
- this
|
|
|
+ vm
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- callHook(this, 'beforeMount')
|
|
|
- this._watcher = new Watcher(this, this._render, this._update)
|
|
|
- this._update(this._watcher.value)
|
|
|
- this._isMounted = true
|
|
|
+ callHook(vm, 'beforeMount')
|
|
|
+ vm._watcher = new Watcher(vm, vm._render, vm._update)
|
|
|
+ vm._update(vm._watcher.value)
|
|
|
+ vm._isMounted = true
|
|
|
// root instance, call mounted on self
|
|
|
- if (this.$root === this) {
|
|
|
- callHook(this, 'mounted')
|
|
|
+ if (vm.$root === vm) {
|
|
|
+ callHook(vm, 'mounted')
|
|
|
}
|
|
|
- return this
|
|
|
+ return vm
|
|
|
}
|
|
|
|
|
|
Vue.prototype._update = function (vnode: VNode) {
|
|
|
- if (this._isMounted) {
|
|
|
- callHook(this, 'beforeUpdate')
|
|
|
+ const vm: Component = this
|
|
|
+ if (vm._isMounted) {
|
|
|
+ callHook(vm, 'beforeUpdate')
|
|
|
}
|
|
|
- if (!this._vnode) {
|
|
|
+ if (!vm._vnode) {
|
|
|
// Vue.prototype.__patch__ is injected in entry points
|
|
|
// based on the rendering backend used.
|
|
|
- this.$el = this.__patch__(this.$el, vnode)
|
|
|
+ vm.$el = vm.__patch__(vm.$el, vnode)
|
|
|
} else {
|
|
|
- this.$el = this.__patch__(this._vnode, vnode)
|
|
|
+ vm.$el = vm.__patch__(vm._vnode, vnode)
|
|
|
}
|
|
|
- this._vnode = vnode
|
|
|
+ vm._vnode = vnode
|
|
|
// update parent vnode element after patch
|
|
|
- const parentNode = this.$options._parentVnode
|
|
|
+ const parentNode = vm.$options._parentVnode
|
|
|
if (parentNode) {
|
|
|
- parentNode.elm = this.$el
|
|
|
+ parentNode.elm = vm.$el
|
|
|
}
|
|
|
- if (this._isMounted) {
|
|
|
- callHook(this, 'updated')
|
|
|
+ if (vm._isMounted) {
|
|
|
+ callHook(vm, 'updated')
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -83,66 +84,65 @@ export function lifecycleMixin (Vue: Class<Vue>) {
|
|
|
parentVnode: VNode,
|
|
|
renderChildren: Array<VNode> | () => Array<VNode>
|
|
|
) {
|
|
|
- this.$options._parentVnode = parentVnode
|
|
|
- this.$options._renderChildren = renderChildren
|
|
|
+ const vm: Component = this
|
|
|
+ vm.$options._parentVnode = parentVnode
|
|
|
+ vm.$options._renderChildren = renderChildren
|
|
|
// update props
|
|
|
- if (propsData && this.$options.props) {
|
|
|
+ if (propsData && vm.$options.props) {
|
|
|
observerState.shouldConvert = false
|
|
|
- const propKeys = this.$options.propKeys
|
|
|
+ const propKeys = vm.$options.propKeys
|
|
|
for (let i = 0; i < propKeys.length; i++) {
|
|
|
const key = propKeys[i]
|
|
|
- this[key] = validateProp(this, key, propsData)
|
|
|
+ vm[key] = validateProp(vm, key, propsData)
|
|
|
}
|
|
|
observerState.shouldConvert = true
|
|
|
}
|
|
|
// update listeners
|
|
|
if (listeners) {
|
|
|
- const oldListeners = this.$options._parentListeners
|
|
|
- this.$options._parentListeners = listeners
|
|
|
+ const oldListeners = vm.$options._parentListeners
|
|
|
+ vm.$options._parentListeners = listeners
|
|
|
updateListeners(listeners, oldListeners || {}, (event, handler) => {
|
|
|
- this.$on(event, handler)
|
|
|
+ vm.$on(event, handler)
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Vue.prototype.$forceUpdate = function () {
|
|
|
- this._watcher.update()
|
|
|
+ const vm: Component = this
|
|
|
+ vm._watcher.update()
|
|
|
}
|
|
|
|
|
|
Vue.prototype.$destroy = function () {
|
|
|
- if (this._isDestroyed) {
|
|
|
+ const vm: Component = this
|
|
|
+ if (vm._isDestroyed) {
|
|
|
return
|
|
|
}
|
|
|
- callHook(this, 'beforeDestroy')
|
|
|
- this._isBeingDestroyed = true
|
|
|
+ callHook(vm, 'beforeDestroy')
|
|
|
+ vm._isBeingDestroyed = true
|
|
|
// remove self from parent
|
|
|
- const parent = this.$parent
|
|
|
+ const parent = vm.$parent
|
|
|
if (parent && !parent._isBeingDestroyed) {
|
|
|
- remove(parent.$children, this)
|
|
|
- }
|
|
|
- // unregister ref
|
|
|
- if (this._ref) {
|
|
|
- this._context.$refs[this._ref] = undefined
|
|
|
+ remove(parent.$children, vm)
|
|
|
}
|
|
|
// teardown watchers
|
|
|
- let i = this._watchers.length
|
|
|
+ let i = vm._watchers.length
|
|
|
while (i--) {
|
|
|
- this._watchers[i].teardown()
|
|
|
+ vm._watchers[i].teardown()
|
|
|
}
|
|
|
// remove reference from data ob
|
|
|
// frozen object may not have observer.
|
|
|
- if (this._data.__ob__) {
|
|
|
- this._data.__ob__.removeVm(this)
|
|
|
+ if (vm._data.__ob__) {
|
|
|
+ vm._data.__ob__.removeVm(vm)
|
|
|
}
|
|
|
// call the last hook...
|
|
|
- this._isDestroyed = true
|
|
|
- callHook(this, 'destroyed')
|
|
|
+ vm._isDestroyed = true
|
|
|
+ callHook(vm, 'destroyed')
|
|
|
// turn off all instance listeners.
|
|
|
- this.$off()
|
|
|
+ vm.$off()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function callHook (vm: Vue, hook: string) {
|
|
|
+export function callHook (vm: Component, hook: string) {
|
|
|
vm.$emit('pre-hook:' + hook)
|
|
|
const handlers = vm.$options[hook]
|
|
|
if (handlers) {
|