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
Copy file name to clipboardExpand all lines: src/guide/data-methods.md
+26-26Lines changed: 26 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# Data Properties and Methods
1
+
# データプロパティとメソッド
2
2
3
-
## Data Properties
3
+
## データプロパティ
4
4
5
-
The`data`option for a component is a function. Vue calls this function as part of creating a new component instance. It should return an object, which Vue will then wrap in its reactivity system and store on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly via the component instance:
//Assigning a value to vm.count will also update $data.count
19
+
// vm.count に値を代入すると、 $data.count も更新
20
20
vm.count=5
21
21
console.log(vm.$data.count) // => 5
22
22
23
-
// ... and vice-versa
23
+
// ... 逆もまた同様
24
24
vm.$data.count=6
25
25
console.log(vm.count) // => 6
26
26
```
27
27
28
-
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data`function. Where necessary, use `null`, `undefined`or some other placeholder value for properties where the desired value isn't yet available.
It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data`object, it won't automatically be tracked by [Vue's reactivity system](reactivity.html).
Vue uses a `$`prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_`for internal properties. You should avoid using names for top-level `data`properties that start with either of these characters.
32
+
Vue は、コンポーネントのインスタンスを介して自身のビルトイン API を公開する際に、 `$`をプレフィックスに使います。 また、内部プロパティのために `_`を予約しています。トップレベルの `data`プロパティの名前に、これらの文字からはじまる名前を使うことは避けるべきです。
33
33
34
-
## Methods
34
+
## メソッド
35
35
36
-
To add methods to a component instance we use the `methods`option. This should be an object containing the desired methods:
Vue automatically binds the`this`value for `methods` so that it always refers to the component instance. This ensures that a method retains the correct `this`value if it's used as an event listener or callback. You should avoid using arrow functions when defining `methods`, as that prevents Vue from binding the appropriate `this` value.
Just like all other properties of the component instance, the `methods`are accessible from within the component's template. Inside a template they are most commonly used as event listeners:
It is also possible to call a method directly from a template. As we'll see shortly, it's usually better to use a [computed property](computed.html)instead. However, using a method can be useful in scenarios where computed properties aren't a viable option. You can call a method anywhere that a template supports JavaScript expressions:
If the methods `toTitleDate`or`formatDate`access any reactive data then it will be tracked as a rendering dependency, just as if it had been used in the template directly.
Methods called from a template should not have any side effects, such as changing data or triggering asynchronous processes. If you find yourself tempted to do that you should probably use a [lifecycle hook](instance.html#lifecycle-hooks)instead.
However, this approach is potentially problematic for components that are reused because they'll all share the same debounced function. To keep the component instances independent from each other, we can add the debounced function in the `created`lifecycle hook:
0 commit comments