Skip to content

Commit

Permalink
composition api helpersとuseIdに関するページの翻訳対応 (#2278)
Browse files Browse the repository at this point in the history
* translate: useId

* create composition-api-helpers

* useIdを除く翻訳

* useIdをcomposition-api-helpersへ移動

* fix: review
  • Loading branch information
yamanoku authored Sep 7, 2024
1 parent d17ade9 commit 4e20810
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ export const sidebar: ThemeConfig['sidebar'] = {
{
text: '依存関係の注入',
link: '/api/composition-api-dependency-injection'
},
{
text: 'ヘルパー',
link: '/api/composition-api-helpers'
}
]
},
Expand Down
19 changes: 18 additions & 1 deletion src/api/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,23 @@ Vue からの実行時警告に対して、カスタムハンドラーを割り

- **参照** [コンポーネントインスタンス - `$options`](/api/component-instance#options)

## app.config.idPrefix <sup class="vt-badge" data-text="3.5+" /> {#app-config-idprefix}

アプリケーション内で [useId()](/api/composition-api-helpers#useid) から生成されるすべての ID に対してプレフィックスを設定します。

- **型:** `string`
- **デフォルト:** `undefined`
- ****

```js
app.config.idPrefix = 'my-app'
```
```js
// コンポーネント内:
const id1 = useId() // 'my-app:0'
const id2 = useId() // 'my-app:1'
```

## app.config.throwUnhandledErrorInProduction <sup class="vt-badge" data-text="3.5+" /> {#app-config-throwunhandlederrorinproduction}

プロダクションモードで未処理のエラーを強制的にスローします。
Expand All @@ -639,4 +656,4 @@ Vue からの実行時警告に対して、カスタムハンドラーを割り

- プロダクションでは、エンドユーザーへの影響を最小限に抑えるため、エラーはコンソールにログ出力されるだけです。ただし、これによりプロダクションでのみ発生するエラーがエラー監視サービスで捕捉されない可能性があります。

`app.config.throwUnhandledErrorInProduction``true` に設定することで、プロダクションモードでも未処理のエラーがスローされるようになります。
`app.config.throwUnhandledErrorInProduction``true` に設定することで、プロダクションモードでも未処理のエラーがスローされるようになります。
129 changes: 129 additions & 0 deletions src/api/composition-api-helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Composition API: ヘルパー {#composition-api-helpers}

## useAttrs() {#useattrs}

現在のコンポーネントの[フォールスルー属性](/guide/components/attrs#fallthrough-attributes)を含む、[Setup Context](/api/composition-api-setup#setup-context) からの `attrs` オブジェクトを返します。これは Setup Context オブジェクトが利用できない `<script setup>` での利用を意図しています。

- ****

```ts
function useAttrs(): Record<string, unknown>
```

## useSlots() {#useslots}

仮想 DOM ノードを返す呼び出し可能な関数として、親に渡されたスロットを含む [Setup Context](/api/composition-api-setup#setup-context) から `slots` オブジェクトを返します。これは Setup Context オブジェクトが利用できない `<script setup>` での利用を意図しています。

TypeScript を使用する場合は、代わりに [`defineSlots()`](/api/sfc-script-setup#defineslots) を優先すべきです。

- ****

```ts
function useSlots(): Record<string, (...args: any[]) => VNode[]>
```

## useModel() {#usemodel}

これは、[`defineModel()`](/api/sfc-script-setup#definemodel) を動かすための基礎となるヘルパーです。`<script setup>` を使用する場合は、`defineModel()` を使用することを推奨します。

- 3.4+から使用可能

- ****

```ts
function useModel(
props: Record<string, any>,
key: string,
options?: DefineModelOptions
)
type DefineModelOptions<T = any> = {
get?: (v: T) => any
set?: (v: T) => any
}
```

- ****

```js
export default {
props: ['count'],
emits: ['update:count'],
setup(props) {
const msg = useModel(props, 'count')
msg.value = 1
}
}
```

- **詳細**

`useModel()` は、例えば生の `setup()` 関数を使用する場合など、SFC 以外のコンポーネントで使用することができます。第一引数として `props` オブジェクトを、第二引数としてモデル名を指定します。省略可能な第三引数は、結果の ref モデルに対してカスタムゲッターとカスタムセッターを宣言するために使われます。`defineModel()` とは異なり、propsemits の宣言は自身で行うことに注意してください。

## useTemplateRef() <sup class="vt-badge" data-text="3.5+" /> {#usetemplateref}

一致する ref 属性を持つテンプレート要素、またはコンポーネントと値が同期される shallow ref を返します。

- ****

```ts
function useTemplateRef<T>(key: string): Readonly<ShallowRef<T | null>>
```

- ****

```vue
<script setup>
import { useTemplateRef, onMounted } from 'vue'
const inputRef = useTemplateRef('input')
onMounted(() => {
inputRef.value.focus()
})
</script>
<template>
<input ref="input" />
</template>
```

- **参照**
- [ガイド - テンプレート参照](/guide/essentials/template-refs)
- [ガイド - テンプレート参照の型付け](/guide/typescript/composition-api#typing-template-refs) <sup class="vt-badge ts" />
- [ガイド - コンポーネントのテンプレート参照の型付け](/guide/typescript/composition-api#typing-component-template-refs) <sup class="vt-badge ts" />

## useId() <sup class="vt-badge" data-text="3.5+" /> {#useid}

アクセシビリティ属性やフォーム要素に対して、アプリケーションごとに一意な ID を生成するために使用します。

- ****

```ts
function useId(): string
```

- ****

```vue
<script setup>
import { useId } from 'vue'
const id = useId()
</script>
<template>
<form>
<label :for="id">Name:</label>
<input :id="id" type="text" />
</form>
</template>
```

- **詳細**

`useId()` で生成された ID はアプリケーションごとに一意なものになります。フォーム要素やアクセシビリティ属性の ID を生成するために使用できます。同じコンポーネントで複数回呼び出すと、異なる ID が生成されます。`useId()` を呼び出す同じコンポーネントの複数のインスタンスも、異なる ID を持つことになります。

`useId()` で生成された ID は、サーバとクライアントのレンダー間で安定していることが保証されているため、SSR アプリケーションでもハイドレーションミスマッチを起こすことなく使用できます。

同じページ内に複数の Vue アプリケーションのインスタンスがある場合、[`app.config.idPrefix`](/api/application#app-config-idprefix)を使用して各アプリケーションに ID プレフィックスを与えることで、ID の衝突を避けることができます。

0 comments on commit 4e20810

Please sign in to comment.