You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After the instance is created, we can _mount_ it, passing a container to `mount`method. For example, if we want to mount a Vue application on `<div id="app"></div>`, we should pass `#app`:
Although not strictly associated with the [MVVM pattern](https://en.wikipedia.org/wiki/Model_View_ViewModel), Vue's design was partly inspired by it. As a convention, we often use the variable `vm` (short for ViewModel) to refer to our instance.
When you create an instance, you pass in an **options object**. The majority of this guide describes how you can use these options to create your desired behavior. For reference, you can also browse the full list of options in the [API reference](../api/options-data.html).
A Vue application consists of a **root instance**created with `createApp`, optionally organized into a tree of nested, reusable components. For example, a `todo`app's component tree might look like this:
We'll talk about [the component system](component-basics.html) in detail later. For now, just know that all Vue components are also instances, and so accept the same options object.
When an instance is created, it adds all the properties found in its `data`to[Vue's **reactivity system**](reactivity.html). When the values of those properties change, the view will "react", updating to match the new values.
When this data changes, the view will re-render. It should be noted that properties in `data`are only **reactive**if they existed when the instance was created. That means if you add a new property, like:
Then changes to `b`will not trigger any view updates. If you know you'll need a property later, but it starts out empty or non-existent, you'll need to set some initial value. For example:
The only exception to this being the use of `Object.freeze()`, which prevents existing properties from being changed, which also means the reactivity system can't _track_ changes.
In addition to data properties, instances expose a number of useful instance properties and methods. These are prefixed with `$`to differentiate them from user-defined properties. For example:
Each instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it also runs functions called **lifecycle hooks**, giving users the opportunity to add their own code at specific stages.
121
+
それぞれのインスタンスは、作成時に一連の初期化の手順を踏みます。例えば、データの監視、テンプレートのコンパイル、インスタンスの DOM へのマウント、データ変更時の DOM の変更を準備する必要があります。この中でインスタンスは、特定の段階にコードを追加する機会をユーザに与えるために、**ライフサイクルフック (lifecycle hooks)** と呼ばれる関数を実行します。
124
122
125
-
For example, the [created](../api/options-lifecycle-hooks.html#created)hook can be used to run code after an instance is created:
There are also other hooks which will be called at different stages of the instance's lifecycle, such as [mounted](../api/options-lifecycle-hooks.html#mounted), [updated](../api/options-lifecycle-hooks.html#updated), and [unmounted](../api/options-lifecycle-hooks.html#unmounted). All lifecycle hooks are called with their `this`context pointing to the current active instance invoking it.
Don't use [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) on an options property or callback, such as `created: () => console.log(this.a)`or`vm.$watch('a', newValue => this.myMethod())`. Since an arrow function doesn't have a `this`, `this`will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as `Uncaught TypeError: Cannot read property of undefined`or`Uncaught TypeError: this.myMethod is not a function`.
142
+
[アロー関数](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions)をオプションのプロパティやコールバックに使用しないでください。これは例えば、`created: () => console.log(this.a)`や`vm.$watch('a', newValue => this.myMethod())` のようなことです。アロー関数は `this` を持たないため、`this`は他の変数のように親のスコープ内を辞書探索され、しばしば `Uncaught TypeError: Cannot read property of undefined`や`Uncaught TypeError: this.myMethod is not a function` のようなエラーを起こします。
145
143
:::
146
144
147
-
## Lifecycle Diagram
145
+
## ライフサイクルダイアグラム
148
146
149
-
Below is a diagram for the instance lifecycle. You don't need to fully understand everything going on right now, but as you learn and build more, it will be a useful reference.
0 commit comments