|
1 |
| -# アプリケーションインスタンス |
| 1 | +# Application & Component Instances |
2 | 2 |
|
3 |
| -## インスタンスの作成 |
| 3 | +## Creating an Application Instance |
4 | 4 |
|
5 | 5 | 全ての Vue アプリケーションは `createApp` 関数で新しい **アプリケーションインスタンス (application instance)** を作成するところから始まります:
|
6 | 6 |
|
7 | 7 | ```js
|
8 |
| -Vue.createApp(/* options */) |
| 8 | +const app = Vue.createApp({ /* options */ }) |
9 | 9 | ```
|
10 | 10 |
|
11 |
| -インスタンスが作成されたあと、コンテナを `mount` メソッドに渡すことで、これを _マウント_ できます。例えば、Vue アプリケーションを `<div id="app"></div>` にマウントしたいときは、`#app` を渡します: |
| 11 | +The application instance is used to register 'globals' that can then be used by components within that application. We'll discuss that in detail later in the guide but as a quick example: |
12 | 12 |
|
13 | 13 | ```js
|
14 |
| -Vue.createApp(/* options */).mount('#app') |
| 14 | +const app = Vue.createApp({}) |
| 15 | +app.component('SearchInput', SearchInputComponent) |
| 16 | +app.directive('focus', FocusDirective) |
| 17 | +app.use(LocalePlugin) |
15 | 18 | ```
|
16 | 19 |
|
17 |
| -[MVVM パターン](https://ja.wikipedia.org/wiki/Model_View_ViewModel)に厳密に関連づけられているわけではないにもかかわらず、Vue の設計は部分的にその影響を受けています。慣例的に、私たちはインスタンスを参照するのに変数 `vm`(ViewModel の短縮形)を使用します。 |
18 |
| - |
19 |
| -インスタンスを作成する際には、 **オプションオブジェクト (options object)** を渡すことができます。このガイドの多くは、意図した挙動を作るためにこれらのオプションをどのように使うことができるかを解説します。全てのオプションのリストは [API リファレンス](../api/options-data.html)で読むこともできます。 |
20 |
| - |
21 |
| -Vue アプリケーションは、`createApp` で作られたひとつの **ルートインスタンス (root instance)** と、入れ子になった再利用可能なコンポーネントのツリーから成ります。例えば、`todo` アプリケーションのコンポーネントツリーはこのようになるでしょう: |
22 |
| - |
23 |
| -``` |
24 |
| -Root Instance |
25 |
| -└─ TodoList |
26 |
| - ├─ TodoItem |
27 |
| - │ ├─ DeleteTodoButton |
28 |
| - │ └─ EditTodoButton |
29 |
| - └─ TodoListFooter |
30 |
| - ├─ ClearTodosButton |
31 |
| - └─ TodoListStatistics |
32 |
| -``` |
33 |
| - |
34 |
| -[コンポーネントシステム](component-basics.html)の詳細は後ほど扱います。いまは、全てのVue コンポーネントもまたインスタンスであり、同じようなオプションオブジェクトを受け入れることだけを知っておいてください。 |
35 |
| - |
36 |
| -## データとメソッド |
37 |
| - |
38 |
| -インスタンスは作成時に、`data` で見つけられる全てのプロパティを [Vue の **リアクティブシステム (reactive system)**](reactivity.html)に追加します。これらのプロパティの値が変わると、ビューは"反応 (react)"し、新しい値に追従します。 |
| 20 | +Most of the methods exposed by the application instance return that same instance, allowing for chaining: |
39 | 21 |
|
40 | 22 | ```js
|
41 |
| -// データオブジェクト |
42 |
| -const data = { a: 1 } |
| 23 | +Vue.createApp({}) |
| 24 | + .component('SearchInput', SearchInputComponent) |
| 25 | + .directive('focus', FocusDirective) |
| 26 | + .use(LocalePlugin) |
| 27 | +``` |
43 | 28 |
|
44 |
| -// オブジェクトがルートインスタンスに追加されます |
45 |
| -const vm = Vue.createApp({ |
46 |
| - data() { |
47 |
| - return data |
48 |
| - } |
49 |
| -}).mount('#app') |
| 29 | +You can browse the full application API in the [API reference](../api/application-api.html). |
50 | 30 |
|
51 |
| -// インスタンス上のプロパティを取得すると、元のデータのプロパティが返されます |
52 |
| -vm.a === data.a // => true |
| 31 | +## The Root Component |
53 | 32 |
|
54 |
| -// インスタンス上のプロパティへの代入は、元のデータにも影響されます |
55 |
| -vm.a = 2 |
56 |
| -data.a // => 2 |
57 |
| -``` |
| 33 | +The options passed to `createApp` are used to configure the **root component**. That component is used as the starting point for rendering when we **mount** the application. |
58 | 34 |
|
59 |
| -このデータが変更されると、ビューが再レンダリングされます。`data` のプロパティは、インスタンスが作成時に存在した場合にのみ **リアクティブ (reactive)** です。次のように新しいプロパティを代入すると: |
| 35 | +An application needs to be mounted into a DOM element. For example, if we want to mount a Vue application into `<div id="app"></div>`, we should pass `#app`: |
60 | 36 |
|
61 | 37 | ```js
|
62 |
| -vm.b = 'hi' |
| 38 | +const RootComponent = { /* options */ } |
| 39 | +const app = Vue.createApp(RootComponent) |
| 40 | +const vm = app.mount('#app') |
63 | 41 | ```
|
64 | 42 |
|
65 |
| -`b` への変更はビューへの更新を引き起こしません。あるプロパティがのちに必要であることがわかっているが、最初は空または存在しない場合は、なんらかの初期値を設定する必要があります。例: |
| 43 | +Unlike most of the application methods, `mount` does not return the application. Instead it returns the root component instance. |
66 | 44 |
|
67 |
| -```js |
68 |
| -data() { |
69 |
| - return { |
70 |
| - newTodoText: '', |
71 |
| - visitCount: 0, |
72 |
| - hideCompletedTodos: false, |
73 |
| - todos: [], |
74 |
| - error: null |
75 |
| - } |
76 |
| -} |
77 |
| -``` |
| 45 | +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 a component instance. |
78 | 46 |
|
79 |
| -これに対する唯一の例外は `Object.freeze()` を使用し既存のプロパティが変更されるのを防ぐことです。これはリアクティブシステムが変更を _追跡 (track)_ することができないことも意味します。 |
| 47 | +While all the examples on this page only need a single component, most real applications are organized into a tree of nested, reusable components. For example, a Todo application's component tree might look like this: |
80 | 48 |
|
81 |
| -```js |
82 |
| -const obj = { |
83 |
| - foo: 'bar' |
84 |
| -} |
| 49 | +``` |
| 50 | +Root Component |
| 51 | +└─ TodoList |
| 52 | + ├─ TodoItem |
| 53 | + │ ├─ DeleteTodoButton |
| 54 | + │ └─ EditTodoButton |
| 55 | + └─ TodoListFooter |
| 56 | + ├─ ClearTodosButton |
| 57 | + └─ TodoListStatistics |
| 58 | +``` |
85 | 59 |
|
86 |
| -Object.freeze(obj) |
| 60 | +Each component will have its own component instance, `vm`. For some components, such as `TodoItem`, there will likely be multiple instances rendered at any one time. All of the component instances in this application will share the same application instance. |
87 | 61 |
|
88 |
| -const vm = Vue.createApp({ |
89 |
| - data() { |
90 |
| - return obj |
91 |
| - } |
92 |
| -}).mount('#app') |
93 |
| -``` |
| 62 | +We'll talk about [the component system](component-basics.html) in detail later. For now, just be aware that the root component isn't really any different from any other component. The configuration options are the same, as is the behavior of the corresponding component instance. |
94 | 63 |
|
95 |
| -```html |
96 |
| -<div id="app"> |
97 |
| - <p>{{ foo }}</p> |
98 |
| - <!-- これでは `foo` は更新されません! --> |
99 |
| - <button v-on:click="foo = 'baz'">Change it</button> |
100 |
| -</div> |
101 |
| -``` |
| 64 | +## Component Instance Properties |
102 | 65 |
|
103 |
| -data プロパティに加えて、インスタンスはいくつかの便利なプロパティとメソッドを提供します。これらはユーザ定義のプロパティと区別するため、頭に `$` が付けられています。例: |
| 66 | +Earlier in the guide we met `data` properties. Properties defined in `data` are exposed via the component instance: |
104 | 67 |
|
105 | 68 | ```js
|
106 |
| -const vm = Vue.createApp({ |
| 69 | +const app = Vue.createApp({ |
107 | 70 | data() {
|
108 |
| - return { |
109 |
| - a: 1 |
110 |
| - } |
| 71 | + return { count: 4 } |
111 | 72 | }
|
112 |
| -}).mount('#example') |
| 73 | +}) |
113 | 74 |
|
114 |
| -vm.$data.a // => 1 |
| 75 | +const vm = app.mount('#app') |
| 76 | + |
| 77 | +console.log(vm.count) // => 4 |
115 | 78 | ```
|
116 | 79 |
|
117 |
| -今後、全てのインスタンスプロパティとメソッドのリストを調べるには [API リファレンス](../api/instance-properties.html) を利用できます。 |
| 80 | +There are various other component options that add user-defined properties to the component instance, such as `methods`, `props`, `computed`, `inject` and `setup`. We'll discuss each of these in depth later in the guide. All of the properties of the component instance, no matter how they are defined, will be accessible in the component's template. |
| 81 | + |
| 82 | +Vue also exposes some built-in properties via the component instance, such as `$attrs` and `$emit`. These properties all have a `$` prefix to avoid conflicting with user-defined property names. |
118 | 83 |
|
119 |
| -## インスタンスライフサイクルフック |
| 84 | +## Lifecycle Hooks |
120 | 85 |
|
121 |
| -それぞれのインスタンスは、作成時に一連の初期化の手順を踏みます。例えば、データの監視、テンプレートのコンパイル、インスタンスの DOM へのマウント、データ変更時の DOM の変更を準備する必要があります。この中でインスタンスは、特定の段階にコードを追加する機会をユーザに与えるために、**ライフサイクルフック (lifecycle hooks)** と呼ばれる関数を実行します。 |
| 86 | +Each component 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. |
122 | 87 |
|
123 | 88 | 例えば [created](../api/options-lifecycle-hooks.html#created) フックは、インスタンスの作成後にコードを実行するために使用できます:
|
124 | 89 |
|
125 | 90 | ```js
|
126 | 91 | Vue.createApp({
|
127 | 92 | data() {
|
128 |
| - return { |
129 |
| - a: 1 |
130 |
| - } |
| 93 | + return { count: 1 } |
131 | 94 | },
|
132 | 95 | created() {
|
133 |
| - // `this` は vm インスタンスを指します |
134 |
| - console.log('a is: ' + this.a) // => "a is: 1" |
| 96 | + // `this` points to the vm instance |
| 97 | + console.log('count is: ' + this.count) // => "count is: 1" |
135 | 98 | }
|
136 | 99 | })
|
137 | 100 | ```
|
|
0 commit comments