Last active
September 4, 2021 00:52
-
-
Save uguisu-an/2808b8d051a3ecb853ad6e618ed1422d to your computer and use it in GitHub Desktop.
example: オブジェクトを編集するコンポーネント
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<Example :user.sync="user"></Example> | |
<button @click.prevent="updateUser()">Save</button> | |
<button @click.prevent="revertUser()">Cancel</button> | |
{{ user }} | |
{{ userBackup }} | |
</div> | |
</template> | |
<script lang="ts"> | |
import Vue from "vue"; | |
import Component from "vue-class-component"; | |
import { cloneDeep } from "lodash"; | |
import Example from "./components/Example.vue"; | |
class User { | |
public name: string = ""; | |
public age: number = 0; | |
} | |
@Component({ | |
components: { | |
Example, | |
}, | |
}) | |
export default class App extends Vue { | |
public user = new User(); | |
public userBackup = new User(); | |
public mounted() { | |
this.user = { | |
name: "john doe", | |
age: 42, | |
}; | |
this.userBackup = cloneDeep(this.user); | |
} | |
public updateUser() { | |
this.userBackup = cloneDeep(this.user); | |
} | |
public revertUser() { | |
this.user = cloneDeep(this.userBackup); | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<label> | |
Name | |
<input type="text" v-model="user.name" @input="$emit('update:user', user)" /> | |
</label> | |
<label> | |
Age | |
<input type="text" v-model.number="user.age" @input="$emit('update:user', user)" /> | |
</label> | |
</div> | |
</template> | |
<script lang="ts"> | |
import { Component, Prop, Vue } from "vue-property-decorator"; | |
@Component({}) | |
export default class Example extends Vue { | |
@Prop() user: { | |
name: string; | |
age: number; | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment