Skip to content

Commit ec69fed

Browse files
skirtles-codenaokie
authored andcommitted
Rewrite of instance.md, introducing data-methods.md (#514)
* docs: experimental rewrite of instance.md, introducing data-methods.md * docs: cut material from instance.md that isn't on topic * docs: move data-methods.md to after template-syntax.md * fix: change 'application' to 'component' in template-syntax.md * fix: use bold when introducing new terms in instance.md * docs: rewrite data-methods.md
1 parent 28d515f commit ec69fed

File tree

1 file changed

+52
-89
lines changed

1 file changed

+52
-89
lines changed

src/guide/instance.md

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,100 @@
1-
# アプリケーションインスタンス
1+
# Application & Component Instances
22

3-
## インスタンスの作成
3+
## Creating an Application Instance
44

55
全ての Vue アプリケーションは `createApp` 関数で新しい **アプリケーションインスタンス (application instance)** を作成するところから始まります:
66

77
```js
8-
Vue.createApp(/* options */)
8+
const app = Vue.createApp({ /* options */ })
99
```
1010

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:
1212

1313
```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)
1518
```
1619

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:
3921

4022
```js
41-
// データオブジェクト
42-
const data = { a: 1 }
23+
Vue.createApp({})
24+
.component('SearchInput', SearchInputComponent)
25+
.directive('focus', FocusDirective)
26+
.use(LocalePlugin)
27+
```
4328

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).
5030

51-
// インスタンス上のプロパティを取得すると、元のデータのプロパティが返されます
52-
vm.a === data.a // => true
31+
## The Root Component
5332

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.
5834

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`:
6036

6137
```js
62-
vm.b = 'hi'
38+
const RootComponent = { /* options */ }
39+
const app = Vue.createApp(RootComponent)
40+
const vm = app.mount('#app')
6341
```
6442

65-
`b` への変更はビューへの更新を引き起こしません。あるプロパティがのちに必要であることがわかっているが、最初は空または存在しない場合は、なんらかの初期値を設定する必要があります。例:
43+
Unlike most of the application methods, `mount` does not return the application. Instead it returns the root component instance.
6644

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.
7846

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:
8048

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+
```
8559

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.
8761

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.
9463

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
10265

103-
data プロパティに加えて、インスタンスはいくつかの便利なプロパティとメソッドを提供します。これらはユーザ定義のプロパティと区別するため、頭に `$` が付けられています。例:
66+
Earlier in the guide we met `data` properties. Properties defined in `data` are exposed via the component instance:
10467

10568
```js
106-
const vm = Vue.createApp({
69+
const app = Vue.createApp({
10770
data() {
108-
return {
109-
a: 1
110-
}
71+
return { count: 4 }
11172
}
112-
}).mount('#example')
73+
})
11374

114-
vm.$data.a // => 1
75+
const vm = app.mount('#app')
76+
77+
console.log(vm.count) // => 4
11578
```
11679

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.
11883

119-
## インスタンスライフサイクルフック
84+
## Lifecycle Hooks
12085

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.
12287

12388
例えば [created](../api/options-lifecycle-hooks.html#created) フックは、インスタンスの作成後にコードを実行するために使用できます:
12489

12590
```js
12691
Vue.createApp({
12792
data() {
128-
return {
129-
a: 1
130-
}
93+
return { count: 1 }
13194
},
13295
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"
13598
}
13699
})
137100
```

0 commit comments

Comments
 (0)