Initialization: What Happens During new Vue
🎯 Introduction
Every Vue project starts with new Vue({...}). But what does Vue do behind that line, and in what order?
In this article, we trace the main initialization path in Vue 2.7.16. By the end, you will be able to describe the sequence from new Vue to rendering and explain why data is unavailable in beforeCreate but available in created.
🧱 Where Is the Vue Constructor?
The entry-point constructor is defined in src/core/instance/index.js. Its simplified form is only a few lines:
// Simplified from src/core/instance/index.js
function Vue(options) {
this._init(options);
}
initMixin(Vue); // Add _init
stateMixin(Vue); // Add $data, $props, $set, $delete, and $watch
eventsMixin(Vue); // Add $on, $once, $off, and $emit
lifecycleMixin(Vue); // Add _update, $forceUpdate, and $destroy
renderMixin(Vue); // Add _render, $nextTick, and other rendering methods
Notice that Vue 2 uses a function rather than an ES6 class. Why?
A function makes it straightforward to split functionality across modules. Each mixin adds a group of methods to Vue.prototype and lives in a separate file. For example, eventsMixin adds event methods, while lifecycleMixin adds lifecycle methods. Putting all these methods in one class would produce a large file that is difficult to maintain.
Although the constructor itself only calls this._init(options), the implementation lives in these mixins. The first step in new Vue is calling _init.
🔍 What Does _init Do?
Defined in src/core/instance/init.js, _init coordinates initialization. Here is its simplified core flow:
// Simplified from src/core/instance/init.js
Vue.prototype._init = function (options) {
const vm = this;
// 1. Merge user options with the constructor's default options
vm.$options = mergeOptions(Vue.options, options, vm);
// 2. Initialize lifecycle relationships: $parent, $children, $refs, etc.
initLifecycle(vm);
// 3. Initialize event listeners attached by the parent component
initEvents(vm);
// 4. Initialize rendering: $slots, $scopedSlots, $createElement, etc.
initRender(vm);
// 5. Call beforeCreate
callHook(vm, 'beforeCreate');
// 6. Initialize injections
initInjections(vm);
// 7. Initialize state: props, methods, data, computed, watch
initState(vm);
// 8. Initialize provide
initProvide(vm);
// 9. Call created
callHook(vm, 'created');
// 10. Mount automatically if el was provided
if (vm.$options.el) {
vm.$mount(vm.$options.el);
}
};
This sequence is central to the article. Pay particular attention to the relative positions of steps 5, 7, and 9:
callHook(vm, 'beforeCreate')runs at step 5.initStatehas not run yet, so data and methods have not been added to the instance.initState(vm)runs at step 7, initializing data, methods, and computed properties.callHook(vm, 'created')runs at step 9, when data is available.
This is why data is unavailable in beforeCreate but available in created: it follows directly from execution order. initState runs between the two hooks.
_init sequence, the order follows naturally: each hook runs where its call appears in the code.⚡ The Order Inside initState
The initState call at step 7 also follows a fixed order, defined in src/core/instance/state.js:
// Simplified from src/core/instance/state.js
function initState(vm) {
const opts = vm.$options;
if (opts.props) initProps(vm, opts.props);
if (opts.methods) initMethods(vm, opts.methods);
if (opts.data) initData(vm);
if (opts.computed) initComputed(vm, opts.computed);
if (opts.watch) initWatch(vm, opts.watch);
}
The order is props → methods → data → computed → watch. It reflects dependencies:
- props before data: initial data often references props, as in
data() { return { count: this.start } }. If data were initialized first,this.startwould not exist yet. - methods before data: data may call a method, as in
data() { return { list: this.getDefaultList() } }, so methods must already be available. - computed and watch last: they typically depend on data and props, so those must be ready first.
Now consider an important operation inside initData:
// Simplified from src/core/instance/state.js
function initData(vm) {
let data = vm.$options.data;
data = vm._data = typeof data === 'function' ? data.call(vm, vm) : data || {};
// Proxy data properties onto vm so they are accessible as this.xxx
proxy(vm, '_data', key);
// Make data reactive
observe(data);
}
Two details matter here. First, proxy exposes this._data.xxx as this.xxx, so application code can omit _data. Second, the final observe(data) call establishes reactivity. The next article explores observe; for now, remember that it runs during initData.
🎬 $mount and the Render Watcher
At the end of _init, Vue calls vm.$mount(el) if el was provided.
In the full build, which includes the compiler, $mount is defined in src/platforms/web/entry-runtime-with-compiler.js. It compiles template (or the mount element's outerHTML) into a render function, then delegates mounting to mountComponent:
// Simplified from src/platforms/web/entry-runtime-with-compiler.js
Vue.prototype.$mount = function (el) {
el = document.querySelector(el);
const options = this.$options;
// Compile the template if no render function exists
if (!options.render) {
const { render } = compileToFunctions(options.template || el.outerHTML);
options.render = render;
}
// Delegate to runtime mounting
return mountComponent(this, el);
};
A simplified mountComponent, from src/core/instance/lifecycle.js, looks like this:
// Simplified from src/core/instance/lifecycle.js
function mountComponent(vm, el) {
vm.$el = el;
callHook(vm, 'beforeMount');
const updateComponent = () => {
vm._update(vm._render());
};
// Create the render watcher, which immediately runs updateComponent for the initial render
new Watcher(vm, updateComponent);
callHook(vm, 'mounted');
return vm;
}
The key function is updateComponent, which performs two operations:
vm._render(): execute the render function to produce the virtual DOM (a vnode).vm._update(vnode): patch the vnode into the real DOM, creating all elements on the initial render.
new Watcher(vm, updateComponent) creates the render watcher. It immediately executes updateComponent for the initial render. During execution, it tracks the data properties being read. When those properties change, it runs updateComponent again, updating the page automatically. This is how data drives the view; the reactivity article explains the details.
Notice the hook positions again: beforeMount runs before watcher creation, when the DOM has not been rendered, and mounted runs afterward, when rendering is complete.
🧵 The Complete Sequence
Putting the steps together, initialization from new Vue to the rendered page follows this order:
new Vue(options)
│
▼
_init(options)
├─ Merge options
├─ initLifecycle / initEvents / initRender
├─ ▶ beforeCreate (data and methods unavailable)
├─ initInjections
├─ initState: props → methods → data (observe establishes reactivity) → computed → watch
├─ initProvide
├─ ▶ created (data and methods available; page not rendered yet)
│
▼
$mount(el) (called automatically when el is provided)
├─ Compile template into a render function
│
▼
mountComponent
├─ ▶ beforeMount
├─ new Watcher(vm, updateComponent): _render() produces a vnode; _update() renders it
└─ ▶ mounted (rendering complete; DOM can be accessed)
Remember three boundaries: initState runs between beforeCreate and created; new Watcher runs between beforeMount and mounted; and template compilation (template → render) happens after created but before beforeMount.
❓ Knowledge Check
Q1: What can I do in beforeCreate?
At this point, data and methods are not initialized, so options are limited. Typical uses include preparation independent of instance data or early global logic injected by plugins. Application code rarely needs this hook.
Q2: Can I manipulate the DOM in created?
No. $mount has not started, so the instance has not rendered any elements and this.$el is not available. Use mounted to work with the rendered DOM.
Q3: Why should data be a function that returns an object?
Each reused component instance executes data.call(vm, vm) to obtain a fresh object, keeping instances independent. A shared object would make all instances share the same data. A root instance created with new Vue can use an object because it does not have this component reuse requirement.
Q4: Will Vue render automatically without el?
No. The end of _init checks vm.$options.el. Without it, initialization stops after created until you call vm.$mount('#app') manually. The remaining sequence is the same.
Q5: Which takes priority when template, render, and el are all present?
$mount uses an existing render function first. Otherwise, it compiles template, or the mount element's outerHTML if no template exists. The priority is render > template > el.
🧪 Exercise
- Using Node.js v22, install
npm i vue@2.7.16and load Vue in a page (or use a CDN for 2.7.16). Create a root instance and logthis.count(defined in data) anddocument.querySelector('#app').innerHTMLinbeforeCreate,created,beforeMount, andmounted. Observe when data becomes available and when rendered content appears.
<script>
export default {
data() {
return { count: 1 };
},
beforeCreate() {
// Write your code here: log this.count and inspect the result
},
created() {
// Write your code here: log this.count and the contents of #app
},
mounted() {
// Write your code here: log #app again and compare it with created
},
};
</script>
- Using the flowchart as a reference, write the
_initsequence from memory on paper and mark where the four hooks run.
🎉 You now understand Vue 2's main initialization path! Next, we will explore observe to see how reactivity is established.
