diff --git a/src/guide/class-and-style.md b/src/guide/class-and-style.md index 0febb18f..66a26b04 100644 --- a/src/guide/class-and-style.md +++ b/src/guide/class-and-style.md @@ -1,20 +1,20 @@ -# Class and Style Bindings +# クラスとスタイルのバインディング -A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can use `v-bind` to handle them: we only need to calculate a final string with our expressions. However, meddling with string concatenation is annoying and error-prone. For this reason, Vue provides special enhancements when `v-bind` is used with `class` and `style`. In addition to strings, the expressions can also evaluate to objects or arrays. +データバインディングに一般に求められることの 1 つは、要素のクラスリストとインラインスタイルを操作することです。それらはどちらも属性であるため、`v-bind` を使って扱うことができます。最終的な文字列を式で計算するだけです。しかしながら、文字列の連結に手を出すのは煩わしく、エラーのもとです。そのため、Vue は `v-bind` が `class` と `style` と一緒に使われるとき、特別な拡張機能を提供します。文字列だけではなく、式はオブジェクトまたは配列を返すことができます。 -## Binding HTML Classes +## HTML クラスのバインディング -### Object Syntax +### オブジェクト構文 -We can pass an object to `:class` (short for `v-bind:class`) to dynamically toggle classes: +`:class` (`v-bind:class` の略) にオブジェクトを渡すことでクラスを動的に切り替えることができます: ```html
``` -The above syntax means the presence of the `active` class will be determined by the [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) of the data property `isActive`. +上記の構文は、`active` クラスの有無がデータプロパティ `isActive` の[真偽性](https://developer.mozilla.org/ja-JP/docs/Glossary/Truthy)によって決まることを意味しています。 -You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following template: +オブジェクトにさらにフィールドを持たせることで複数のクラスを切り替えることができます。加えて、`:class` ディレクティブはプレーンな `class` 属性と共存できます。つまり、次のようなテンプレートと: ```html ``` -And the following data: +次のようなデータがあったとすると: ```js data() { @@ -34,15 +34,15 @@ data() { } ``` -It will render: +このように描画されます: ```html ``` -When `isActive` or `hasError` changes, the class list will be updated accordingly. For example, if `hasError` becomes `true`, the class list will become `"static active text-danger"`. +`isActive` もしくは `hasError` が変化するとき、クラスリストはそれに応じて更新されます。例えば、`hasError` が `true` になった場合、クラスリストは `"static active text-danger"` になります。 -The bound object doesn't have to be inline: +束縛されるオブジェクトはインラインでなくてもかまいません: ```html @@ -59,7 +59,7 @@ data() { } ``` -This will render the same result. We can also bind to a [computed property](computed.md) that returns an object. This is a common and powerful pattern: +これは同じ結果を描画します。オブジェクトを返す[算出プロパティ](computed.md)に束縛することもできます。これは一般的で強力なパターンです: ```html @@ -82,9 +82,9 @@ computed: { } ``` -### Array Syntax +### 配列構文 -We can pass an array to `:class` to apply a list of classes: +`:class` に配列を渡してクラスのリストを適用することができます: ```html @@ -99,33 +99,33 @@ data() { } ``` -Which will render: +これは次のように描画されます: ```html ``` -If you would like to also toggle a class in the list conditionally, you can do it with a ternary expression: +リスト内のクラスを条件に応じて切り替えたい場合は、三項演算子式を使って実現することができます: ```html ``` -This will always apply `errorClass`, but will only apply `activeClass` when `isActive` is truthy. +この場合 `errorClass` は常に適用されますが、`activeClass` クラスは `isActive` が真と評価されるときだけ適用されます。 -However, this can be a bit verbose if you have multiple conditional classes. That's why it's also possible to use the object syntax inside array syntax: +しかしながら、これでは複数の条件つきクラスがあると少し冗長になってしまいます。そのため、配列構文の内部ではオブジェクト構文を使うこともできます: ```html ``` -### With Components +### コンポーネントにおいて -> This section assumes knowledge of [Vue Components](component-basics.md). Feel free to skip it and come back later. +> このセクションでは、[Vue のコンポーネント](component-basics.md)の知識を前提としています。いったんスキップして後で戻ってきても構いません。 -When you use the `class` attribute on a custom component with a single root element, those classes will be added to this element. Existing classes on this element will not be overwritten. +単一のルート要素を持つカスタムコンポーネントで `class` 属性を使用すると、それらのクラスがこの要素に追加されます。この要素の既存のクラスは上書きされません。 -For example, if you declare this component: +例えば、このコンポーネントを宣言して: ```js const app = Vue.createApp({}) @@ -135,7 +135,7 @@ app.component('my-component', { }) ``` -Then add some classes when using it: +使用するときにいくつかのクラスを追加したとします: ```html