Skip to content

Commit

Permalink
Add note about how to reference generic components (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-es authored Aug 2, 2024
1 parent d47cf89 commit 30c4c8e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/api/sfc-script-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ if (modelModifiers.trim) {
}
```


修飾子が存在する場合、親から読み込んだ値や親に同期して返す値を変換する必要があることが多いです。それを実現するには `get``set` 変換オプションを使用します:

```js
Expand Down Expand Up @@ -470,6 +469,25 @@ defineProps<{
</script>
```

`ref` でジェネリックコンポーネントへの参照を使用する場合、`InstanceType` は動作しないので、[`vue-component-type-helpers`](https://www.npmjs.com/package/vue-component-type-helpers) ライブラリーを使用する必要があります。

```vue
<script
setup
lang="ts"
>
import componentWithoutGenerics from '../component-without-generics.vue';
import genericComponent from '../generic-component.vue';
import type { ComponentExposed } from 'vue-component-type-helpers';
// ジェネリクスのないコンポーネントでは動作します
ref<InstanceType<typeof componentWithoutGenerics>>();
ref<ComponentExposed<typeof genericComponent>>();
```


## 制限 {#restrictions}

- モジュールの実行セマンティクスの違いにより、`<script setup>` 内のコードは、SFC のコンテキストに依存しています。外部の `.js``.ts` ファイルに移動すると、開発者とツールの両方に混乱を招く可能性があります。そのため、**`<script setup>`** は、`src` 属性と一緒に使うことはできません。
Expand Down

0 comments on commit 30c4c8e

Please sign in to comment.