Skip to content

Commit 4892ead

Browse files
authored
Docs > Essentials Guide の更新 (#363)
* fix variable name in code. vuejs/docs@e56c456 * fix import closed quote vuejs/docs@fa2c887 * docs: Update link & fix prop name in typescript-support.md vuejs/docs@64cbc01 * Added template refs typing instruction vuejs/docs@d5c6635
1 parent af2a34c commit 4892ead

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

src/guide/composition-api-provide-inject.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default {
6060
6161
<script>
6262
import { provide } from 'vue'
63-
import MyMarker from './MyMarker.vue
63+
import MyMarker from './MyMarker.vue'
6464
6565
export default {
6666
components: {
@@ -123,7 +123,7 @@ export default {
123123
124124
<script>
125125
import { provide, reactive, ref } from 'vue'
126-
import MyMarker from './MyMarker.vue
126+
import MyMarker from './MyMarker.vue'
127127
128128
export default {
129129
components: {
@@ -159,7 +159,7 @@ export default {
159159
160160
<script>
161161
import { provide, reactive, ref } from 'vue'
162-
import MyMarker from './MyMarker.vue
162+
import MyMarker from './MyMarker.vue'
163163
164164
export default {
165165
components: {
@@ -198,7 +198,7 @@ export default {
198198
199199
<script>
200200
import { provide, reactive, ref } from 'vue'
201-
import MyMarker from './MyMarker.vue
201+
import MyMarker from './MyMarker.vue'
202202
203203
export default {
204204
components: {
@@ -254,7 +254,7 @@ export default {
254254
255255
<script>
256256
import { provide, reactive, readonly, ref } from 'vue'
257-
import MyMarker from './MyMarker.vue
257+
import MyMarker from './MyMarker.vue'
258258
259259
export default {
260260
components: {

src/guide/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Vue.createApp(Counter).mount('#counter')
5555
これで、初めての Vue アプリケーションが作成できました!一見するとただ文字列をレンダリングするだけのテンプレートですが、Vue は内部で多くの処理をおこなっています。データと DOM は関連付けられ、すべてが **リアクティブ(反応的)** になっています。どうすればそれが分かるのでしょうか?以下の例で、`counter` プロパティが 1 秒ごとにインクリメントされたとき、レンダリングされた DOM がどのように変化するかを見てみましょう:
5656

5757
```js{8-10}
58-
const CounterApp = {
58+
const Counter = {
5959
data() {
6060
return {
6161
counter: 0

src/guide/typescript-support.md

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const Component = defineComponent({
131131
})
132132
```
133133

134-
複雑な型や推論の場合、[タイプアサーション (type assertion)](https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions) を使用してキャストすることができます:
134+
複雑な型や推論の場合、[タイプアサーション (type assertion)](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) を使用してキャストすることができます:
135135

136136
```ts
137137
interface Book {
@@ -221,10 +221,10 @@ const Component = defineComponent({
221221
// セッターを持つ算出プロパティのでは、ゲッターにアノテーションが必要です
222222
greetingUppercased: {
223223
get(): string {
224-
return this.greeting.toUpperCase();
224+
return this.greeting.toUpperCase()
225225
},
226226
set(newValue: string) {
227-
this.message = newValue.toUpperCase();
227+
this.message = newValue.toUpperCase()
228228
}
229229
}
230230
}
@@ -260,7 +260,7 @@ const Component = defineComponent({
260260
```
261261

262262
::: warning
263-
TypeScript には、関数式の型推論に [設計上の制限](https://github.com/microsoft/TypeScript/issues/38845) があるため、 `validators` と、オブジェクトや配列の `default` 値に注意する必要があります:
263+
TypeScript には、関数式の型推論に [設計上の制限](https://github.com/microsoft/TypeScript/issues/38845) があるため、 `validator` と、オブジェクトや配列の `default` 値に注意する必要があります:
264264
:::
265265

266266
```ts
@@ -297,7 +297,7 @@ const Component = defineComponent({
297297
})
298298
```
299299

300-
### emits にアノテーションをつける
300+
### Emit にアノテーションをつける
301301

302302
発行されたイベントのペイロードにアノテーションをつけることができます。また、すべての宣言されていない発行されたイベントは、呼び出されたときに型エラーが発生します:
303303

@@ -372,6 +372,77 @@ year.value = 2020 // OKです!
372372
ジェネリックの型が不明の場合、`ref``Ref<T>` にキャストすることを推奨します。
373373
:::
374374

375+
### テンプレート参照を型定義する
376+
377+
ときどき、子コンポーネントのパブリックメソッドを呼び出すために、テンプレート参照にアノテーションをつける必要があるかもしれません。例えば、 `MyModal` という子コンポーネントにモーダルを開くメソッドがあるとします:
378+
379+
```ts
380+
import { defineComponent, ref } from 'vue'
381+
382+
const MyModal = defineComponent({
383+
setup() {
384+
const isContentShown = ref(false)
385+
const open = () => (isContentShown.value = true)
386+
387+
return {
388+
isContentShown,
389+
open
390+
}
391+
}
392+
})
393+
```
394+
395+
この親コンポーネントからテンプレート参照を介して、このメソッドを呼び出したいです:
396+
397+
```ts
398+
import { defineComponent, ref } from 'vue'
399+
400+
const MyModal = defineComponent({
401+
setup() {
402+
const isContentShown = ref(false)
403+
const open = () => (isContentShown.value = true)
404+
405+
return {
406+
isContentShown,
407+
open
408+
}
409+
}
410+
})
411+
412+
const app = defineComponent({
413+
components: {
414+
MyModal
415+
},
416+
template: `
417+
<button @click="openModal">Open from parent</button>
418+
<my-modal ref="modal" />
419+
`,
420+
setup() {
421+
const modal = ref()
422+
const openModal = () => {
423+
modal.value.open()
424+
}
425+
426+
return { modal, openModal }
427+
}
428+
})
429+
```
430+
431+
この方法でも動作しますが、 `MyModal` とその利用可能なメソッドについての型情報がありません。これを解決するためには、 ref を作成するときに `InstanceType` を使う必要があります:
432+
433+
```ts
434+
setup() {
435+
const modal = ref<InstanceType<typeof MyModal>>()
436+
const openModal = () => {
437+
modal.value?.open()
438+
}
439+
440+
return { modal, openModal }
441+
}
442+
```
443+
444+
[オプショナルチェイニング](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) や他の方法を使って、 `modal.value` が未定義でないことの確認が必要であることに注意してください。
445+
375446
### `reactive` を型定義する
376447

377448
`reactive` プロパティを型定義する場合、推論を使用できます:

0 commit comments

Comments
 (0)