私はできる限りこれを説明しようとします。私はコンポーネントを持っています。/resources/
パスと/users/ + uid + /created-resources
パスにデータを投稿し、newPostKey
と更新を使用します。複数のコンポーネントから特定のパスへのデータの投稿
また、QuizBuilder
コンポーネントもあります。私は/resources/ + newPostKey + /quiz/
パスにこのコンポーネントからのデータを投稿したいと思います。しかし、私はResourceInfo
で作成した特定のパスからnewPostKey
またはキーを入手する方法を、QuizBuilder
コンポーネントから取得する方法がわかりません。
ここに2つのコンポーネントがあります。まず、ユーザーがResourceInfo
コンポーネントを使用して情報を追加します。送信したら、QuizBuilder
コンポーネントに移動し、クイズを作成します。
ResourceInfo.vue
export default {
name: 'resource-info',
data() {
return {
header: 'Before you build your quiz we just need some quick info.',
sharedState: store.state,
resource: {
type: '',
title: '',
url: '',
desc: '',
timesPassed: 0,
authorId: store.state.userInfo.uid,
authorName: store.state.userInfo.displayName,
authorImage: store.state.userInfo.photoURL
},
}
},
methods: {
saveToFirebase() {
var newPostKey = firebase.database().ref().child('resources').push().key;
var updates = {};
updates['/resources/' + newPostKey] = this.resource;
updates['/users/' + store.state.userInfo.uid + '/created-resources/' + newPostKey] = this.resource;
// Clear inputs
this.resource.title = '',
this.resource.type = '',
this.resource.desc = '',
this.resource.url = ''
console.log("Saving resource data...")
return firebase.database().ref().update(updates);
}
}
}
QuizBuilder.vue
export default {
name: "quiz-builder",
data() {
return {
questions: [createNewQuestion()],
showQuestions: false
}
},
methods: {
addQuestion() {
this.questions.push(createNewQuestion())
},
addOption (question) {
question.options.push(createNewOption())
},
saveToFirebase (e) {
e.preventDefault();
var questions = this.questions;
this.firebaseRef = db.ref('a/path/here'); // /resources/ + that resources id + /quiz/
this.firebaseRef.push({ // Should I use set or push here?
questions
})
console.log('Saving quiz data...')
}
}
}
それが最初の選択肢です。しかし、私はすべての状態を 'store.js'に保存し、各コンポーネントの状態を' sharedState:store.state'に設定します。私は単一のファイルコンポーネントも使用していますし、コンポーネントを入れ子にしません。 'main.js'は親だけでしょうか? – maxwellgover
@staxwell私の編集時に通知を受けるかどうかはわかりません。それが動作するかどうか私に教えてください。 –
うん。私はstore.jsファイルにキーを格納し、そこからそれを取得してしまった。あなたのすべての協力に感謝します!答えに私を助けた! – maxwellgover