2017-04-05 16 views
0

Firebase JSONデータベースで使用している単純なVueJSアプリがあります。私は、矢印をクリックしてコメントの「投票」を更新できるようにしたい。 (このウェブサイトがアップフォースを持つ方法と同様)。私の機能が実行されます。しかし、VueモデルはFirebaseを更新しないので、リフレッシュ時に投票数が失われます。私CodePenを参照してください:http://codepen.io/Auzy/pen/evowdd/(通常のHTML/CSSの右上にある矢印をクリックして見て、私はパグやスタイラスを使用することに注意してください。)Vue JS Firebaseデータベースにインクリメントした番号を保存する

JS:

// Initialize Firebase 
var config = { 
    databaseURL: "..." 
}; 
const firebaseapp = firebase.initializeApp(config); 
const db = firebaseapp.database(); 
const commentsRef = db.ref('test'); 

const app = new Vue({ 
    el: '#app', 
    data: { 
     comments: [], 
     newComment: { 
      name: '', 
      comment: '', 
      votes: 0 
     }, 
    }, 
    methods: { 
     addComment() { 
      commentsRef.push(this.newComment); 
      this.newComment.name = ''; 
      this.newComment.message = ''; 
      this.newComment.votes = 0; 
     }, 
     vote: function (voteType, comment) { 
      if (voteType === "up") { 
       comment.votes++ 
      } else if (voteType === "down") { 
       if (comment.votes <= -10) { 
        return; 
       } 

       comment.votes--; 
      } 
     }, 
    }, 
    firebase: { 
     comments: commentsRef 
    }, 

}) 
+0

'firebase'プロパティは、標準の' Vue'オプションではありません。

は、ここで追加のノートを使用して新しい方法です。 [vuefire](https://github.com/vuejs/vuefire)を使用していますか?もしそうなら、それはどこですか? – Phil

+0

はい、私はVueFireを使用しています。スクリプトタグは、CodePen JS設定のリソースの下にあります。 – Auzy

答えて

1

さてさて、私は私がきたと信じて理解した。より良い方法があれば答えてください。

incrementVote(comment) { 
    comment.votes++ //increment the vote count 
    const businesschildKey = comment['.key']; //get key of modified comment 
    delete comment['.key']; //Firebase doesn't know how to handle them but can use VueFire to get around that. 
    this.$firebaseRefs.comments.child(businesschildKey).set(comment) //Updates Firebase record matching key 
}, 
関連する問題