LoginSignup
1
0

More than 1 year has passed since last update.

【Vue.js with TypeScript】propsでObjectを渡すときの型の指定方法

Last updated at Posted at 2021-09-12

概要

propsで親componentから子componentにObjectを渡すときの型を、interfaceで指定する方法。

方法

type: Object as () => SampleType, のようにinterfaceを返すメソッドを指定する。

実装例

<template>
  <div id="sample-button">
    <button class="m-3 btn btn-sm btn-primary" @click="clickChildComponent">
      SampleButton
    </button>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import SampleType from "@/types/SampleType";

export default Vue.extend({
  name: "sample",
  props: {
    sample: {
      // SampleTypeというinterfaceをメソッドにして指定する
      type: Object as () => SampleType,
      required: true,
    }
  },
  methods: {
    clickChildComponent(): void {
      console.log(this.sample);
    }
  },
});
</script>

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0