new Vue 发生了什么

new Vue 发生了什么

  new vue 我们从入口分析,我们new 一个实例化对象,是由Funcction实现的,来看一下源码,在src/core/instance/index.js 中。

 

import { initMixin } from './init'
import { stateMixin } from './state'
import { renderMixin } from './render'
import { eventsMixin } from './events'
import { lifecycleMixin } from './lifecycle'
import { warn } from '../util/index'

function Vue ( options) {
if ( process. env. NODE_ENV !== 'production' &&
!( this instanceof Vue)
) {
warn( 'Vue is a constructor and should be called with the `new` keyword')
}
this. _init( options)
}

initMixin( Vue)
stateMixin( Vue)
eventsMixin( Vue)
lifecycleMixin( Vue)
renderMixin( Vue)

由此可以看出 Function 里面 this._init(options), 通过原型的调用 在这里调用
src/core/instance/init.js

Vue. prototype. _init = function ( options?: Object) {
const vm: Component = this
// a uid
vm. _uid = uid++

let startTag, endTag
/* istanbul ignore if */
if ( process. env. NODE_ENV !== 'production' && config. performance && mark) {
startTag = `vue-perf-start: ${ vm. _uid } `
endTag = `vue-perf-end: ${ vm. _uid } `
mark( startTag)
}

// a flag to avoid this being observed
vm. _isVue = true
// merge options
if ( options && options. _isComponent) {
// optimize internal component instantiation
// since dynamic options merging is pretty slow, and none of the
// internal component options needs special treatment.
initInternalComponent( vm, options)
} else {
vm. $options = mergeOptions(
resolveConstructorOptions( vm. constructor),
options || {},
vm
)
}
/* istanbul ignore else */
if ( process. env. NODE_ENV !== 'production') {
initProxy( vm)
} else {
vm. _renderProxy = vm
}
// expose real self
vm. _self = vm
initLifecycle( vm)
initEvents( vm)
initRender( vm)
callHook( vm, 'beforeCreate')
initInjections( vm) // resolve injections before data/props
initState( vm)
initProvide( vm) // resolve provide after data/props
callHook( vm, 'created')

/* istanbul ignore if */
if ( process. env. NODE_ENV !== 'production' && config. performance && mark) {
vm. _name = formatComponentName( vm, false)
mark( endTag)
measure( `vue ${ vm. _name } init`, startTag, endTag)
}

if ( vm. $options. el) {
vm. $mount( vm. $options. el)
}
}

 从中可以看下,初始化生命周期、初始化事件,初始化data/props/render/watch/computed

 

从中可以看出 beforeCreate 在 初始化data前就开始的生命周期,创建data之后 created生命周期

 

转载于:https://www.cnblogs.com/yayaxuping/p/9614451.html

Vue 3中,使用`createApp`函数来创建Vue实例,而不是使用`new Vue()`。`createApp`函数接受一个根组件作为参数,并返回一个应用实例。下面是一个示例代码: ```javascript import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.mount('#app'); ``` 在这个示例中,我们首先导入`createApp`函数和根组件`App`。然后,我们使用`createApp`函数创建一个应用实例,并将根组件作为参数传递给它。最后,我们使用`mount`方法将应用实例挂载到指定的DOM元素上。 需要注意的是,Vue 3中的`createApp`函数返回的是一个应用实例,而不是Vue构造函数本身。因此,我们不再需要使用`new`关键字来创建Vue实例。 #### 引用[.reference_title] - *1* [【vue3系列】vue3快速入门教程](https://blog.csdn.net/qq_42445025/article/details/120749719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue进阶—new Vue()时发生了什么](https://blog.csdn.net/weixin_43974265/article/details/114181405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【vue】关于new Vue()与Vue.use()](https://blog.csdn.net/zhangank/article/details/116742961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值