Vue.js、Vuetify、firebaseを使いwebアプリを作成しております。
ユーザーが自分のプロフィールを編集するページを作っているのですが、行き詰ってしまいました。
「プロフィールを編集」ボタンを押したら入力フォームを表示し編集できるようにし、その際に「更新」ボタンを押したらデータを更新、「キャンセル」ボタンを押したら入力した内容を破棄して戻るようにしたいです。
1回目はキャンセルボタンがきちんと動作するのですが2回目以降はキャンセルが効かずに変更されてしまいます。
まだVueを勉強し始めたばかりなので至らぬ点が多いかとお思いますが、どこをどう直したら良いかご教授いただけると幸いです。宜しくお願い致します。
Vue
1<template> 2 <div> 3 <v-btn flat outline v-if="!isEdit" @click="edit" class="ml-4">プロフィールを編集</v-btn> 4 <v-btn flat outline v-if="isEdit" @click="update" v-bind:disabled="noChange" class="red red--text ml-4">更新</v-btn> 5 <v-btn flat outline v-if="isEdit" @click="cancel">キャンセル</v-btn> 6 <v-layout align-center> 7 <v-flex xs3><v-subheader>名前</v-subheader></v-flex> 8 <v-flex xs9 v-if="!isEdit">{{currentUser.onamae}}</v-flex> 9 <v-flex xs9 v-if="isEdit"> 10 <v-text-field v-model="currentUser.onamae"></v-text-field> 11 </v-flex> 12 </v-layout> 13 </div> 14</template> 15 16<script> 17import firebase from 'firebase' 18import db from '@/fb' 19 20export default { 21 22 data(){ 23 return{ 24 isEdit: false, 25 updated: false, 26 } 27 }, 28 29 methods: { 30 edit(){ 31 this.isEdit = true; 32 this.updated = false; 33 }, 34 35 update(){ 36 this.newUserData = { 37 onamae: this.currentUser.onamae, 38 }; 39 40 this.currentUserEmail = firebase.auth().currentUser.email; //ログイン中ユーザのメールアドレスを取得 41 var currentUserDoc = db.collection('users').doc(this.currentUserEmail); //ログイン中ユーザーのドキュメントを代入 42 currentUserDoc.update(this.newUserData) 43 .then(function(docRef) { 44 console.log("Document written with ID: ", docRef); 45 location.reload(); //ページをリロード 46 }) 47 .catch(function(error) { 48 console.error("Error adding document: ", error); 49 }); 50 }, 51 52 cancel(){ 53 this.isEdit = false; 54 this.$store.commit('setUser', this.oldUserData); 55 }, 56 }, 57 58 computed: { 59 60 changes: function() { 61 const changes = []; 62 const orig = this.oldUserData; 63 const dest = this.currentUser; 64 Object.keys(orig).forEach(function(key) { 65 if (orig[key] != dest[key]) { 66 changes.push({name: key}); 67 } 68 }); 69 return changes; 70 }, 71 72 noChange: function() { 73 // 何か情報がオリジナルと変わったときだけ、更新ボタンをアクティブにする。 74 return !(this.isEdit && this.changes.length > 0); 75 }, 76 77 currentUser: function(){ 78 return this.$store.getters.currentUser; 79 }, 80 81 oldUserData(){ 82 return this.$store.getters.oldUserData; 83 }, 84 } 85} 86 87 88
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/07 02:58