【Vue.js + TypeScript】コンポーネント間のデータやりとりについてのメモ
14509 ワード
概要
ちょっと夏休みとかちょっと冬休みとかあるとすぐ書き方を忘れるのでメモ
親→子にプロパティでデータを渡す(props)
Parent.vue
<template>
<div class="content">
<child :data="text" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
text = "データだよ";
}
</script>
Child.vue
<template>
<div class="content">
{{ text }}
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Prop } from "vue-property-decorator";
@Component({
name: "child"
})
export default class Child extends Vue {
@Prop({ default: "" })
text!: string;
}
</script>
子→親にイベントでデータを渡す(emit)
Parent.vue
<template>
<div class="content">
<child @handle-parent="childEvent" />
{{ text }}
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
text = "";
childEvent(text) {
this.text = text;
}
}
</script>
Child.vue
<template>
<div class="content">
<button @click="handler('データだよ')" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
@Component({
name: "child"
})
export default class Child extends Vue {
handler(text) {
this.$emit("handle-parent", text);
}
}
</script>
親→子のメソッドを呼び出す(refs)
Parent.vue
<template>
<div class="content">
<child ref="child" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
$refs!: {
child: Child
}
mounted() {
this.$refs.child.hello();
}
}
</script>
Child.vue
<template>
<div class="content"> </div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
@Component({
name: "child"
})
export default class Child extends Vue {
hello() {
alert("Hello!");
}
}
</script>
子→親のメソッドを呼び出す(emit)
Parent.vue
<template>
<div class="content">
<child :data="text" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
text = "データだよ";
}
</script>
Child.vue
<template>
<div class="content">
{{ text }}
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Prop } from "vue-property-decorator";
@Component({
name: "child"
})
export default class Child extends Vue {
@Prop({ default: "" })
text!: string;
}
</script>
Parent.vue
<template>
<div class="content">
<child @handle-parent="childEvent" />
{{ text }}
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
text = "";
childEvent(text) {
this.text = text;
}
}
</script>
Child.vue
<template>
<div class="content">
<button @click="handler('データだよ')" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
@Component({
name: "child"
})
export default class Child extends Vue {
handler(text) {
this.$emit("handle-parent", text);
}
}
</script>
親→子のメソッドを呼び出す(refs)
Parent.vue
<template>
<div class="content">
<child ref="child" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
$refs!: {
child: Child
}
mounted() {
this.$refs.child.hello();
}
}
</script>
Child.vue
<template>
<div class="content"> </div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
@Component({
name: "child"
})
export default class Child extends Vue {
hello() {
alert("Hello!");
}
}
</script>
子→親のメソッドを呼び出す(emit)
Parent.vue
<template>
<div class="content">
<child ref="child" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Child from "./Child.vue";
@Component({
name: "parent",
components: {
Child
}
})
export default class Parent extends Vue {
$refs!: {
child: Child
}
mounted() {
this.$refs.child.hello();
}
}
</script>
Child.vue
<template>
<div class="content"> </div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
@Component({
name: "child"
})
export default class Child extends Vue {
hello() {
alert("Hello!");
}
}
</script>
これはデータを渡すときと同じですね!
感想
暗記できません(⌒∇⌒)
Author And Source
この問題について(【Vue.js + TypeScript】コンポーネント間のデータやりとりについてのメモ), 我々は、より多くの情報をここで見つけました https://qiita.com/tsumasakky/items/cd661865cb020024b1b0著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .