Last active
November 9, 2019 01:42
-
-
Save wataruoguchi/2bdc234bc2f91b1b28f84b959d8064f3 to your computer and use it in GitHub Desktop.
VuexFireでNuxt.jsアプリに一瞬でFirestoreを導入する ref: https://qiita.com/wataruoguchi/items/8fdda8e9754658be06be
This file contains hidden or 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
<proj> | |
L src | |
L plugins | |
L components | |
L package.json | |
L ... | |
L functions | |
L public |
This file contains hidden or 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> | |
<h1>firestore contents here</h1> | |
<ul> | |
<li v-for="(user, userIdx) in users" :key="userIdx"> | |
<ul> | |
<li>name: {{ user.name }}</li> | |
<li>email: {{ user.email }}</li> | |
</ul> | |
</li> | |
</ul> | |
+ <div class="form"> | |
+ <h1>I know, but this is the form!</h1> | |
+ <div> | |
+ Name: <input type="text" name="name" v-model="name"> | |
+ </div> | |
+ <div> | |
+ email: <input type="text" name="email" v-model="email"> | |
+ </div> | |
+ <div> | |
+ <button @click="submit">Submit</button> | |
+ </div> | |
+ </div> | |
</div> | |
</template> | |
<script> | |
import { db } from '~/plugins/firebase.js' | |
import { mapGetters } from 'vuex' | |
export default { | |
created: function () { | |
this.$store.dispatch('setUsersRef', db.collection('users')) | |
}, | |
computed: { | |
...mapGetters({ users: 'getUsers' }) | |
}, | |
+ data: function () { | |
+ return { | |
+ name: '', | |
+ email: '', | |
+ } | |
+ }, | |
+ methods: { | |
+ submit: function () { | |
+ const user = { | |
+ name: this.name, | |
+ email: this.email, | |
+ } | |
+ const usersRef = db.collection('users') | |
+ usersRef.add(user) | |
+ this.name = '' | |
+ this.email = '' | |
+ } | |
+ }, | |
} | |
</script> | |
<style> | |
</style> |
This file contains hidden or 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> | |
<h1>firestore contents here</h1> | |
<ul> | |
<li v-for="(user, userIdx) in users" :key="userIdx"> | |
<ul> | |
<li>name: {{ user.name }}</li> | |
<li>email: {{ user.email }}</li> | |
</ul> | |
</li> | |
</ul> | |
+ <div class="form"> | |
+ <h1>I know, but this is the form!</h1> | |
+ <div> | |
+ Name: <input type="text" name="name" v-model="name"> | |
+ </div> | |
+ <div> | |
+ email: <input type="text" name="email" v-model="email"> | |
+ </div> | |
+ <div> | |
+ <button @click="submit">Submit</button> | |
+ </div> | |
+ </div> | |
</div> | |
</template> | |
<script> | |
import { db } from '~/plugins/firebase.js' | |
import { mapGetters } from 'vuex' | |
export default { | |
created: function () { | |
this.$store.dispatch('setUsersRef', db.collection('users')) | |
}, | |
computed: { | |
...mapGetters({ users: 'getUsers' }) | |
}, | |
+ data: function () { | |
+ return { | |
+ name: '', | |
+ email: '', | |
+ } | |
+ }, | |
+ methods: { | |
+ submit: function () { | |
+ const user = { | |
+ name: this.name, | |
+ email: this.email, | |
+ } | |
+ const usersRef = db.collection('users') | |
+ usersRef.add(user) | |
+ this.name = '' | |
+ this.email = '' | |
+ } | |
+ }, | |
} | |
</script> | |
<style> | |
</style> |
This file contains hidden or 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
import firebase from 'firebase' | |
import 'firebase/firestore' | |
const serviceAccount = require('~/../serviceAccountKey.json') | |
firebase.initializeApp({ ...serviceAccount }) | |
const db = firebase.firestore() | |
const settings = { timestampsInSnapshots: true } | |
db.settings(settings) | |
export { db } |
This file contains hidden or 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
import Vuex from 'vuex' | |
import { firebaseMutations, firebaseAction } from 'vuexfire' | |
const createStore = () => { | |
return new Vuex.Store({ | |
state: { | |
users: [], | |
}, | |
mutations: { | |
...firebaseMutations | |
}, | |
actions: { | |
setUsersRef: firebaseAction(({ bindFirebaseRef }, ref) => { | |
bindFirebaseRef('users', ref) | |
}) | |
}, | |
getters: { | |
getUsers: (state) => { | |
return state.users | |
}, | |
}, | |
}) | |
} | |
export default createStore |
This file contains hidden or 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
module.exports = { | |
... | |
plugins: ['~/plugins/firebase'], | |
... | |
} |
This file contains hidden or 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
{ | |
"apiKey": "<API_KEY>", | |
"authDomain": "<PROJECT_ID>.firebaseapp.com", | |
"databaseURL": "https://<DATABASE_NAME>.firebaseio.com", | |
"projectId": "<PROJECT_ID>", | |
"storageBucket": "<BUCKET>.appspot.com", | |
"messagingSenderId": "<SENDER_ID>" | |
} |
This file contains hidden or 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> | |
<h1>firestore contents here</h1> | |
<ul> | |
<li v-for="(user, userIdx) in users" :key="userIdx"> | |
<ul> | |
<li>name: {{ user.name }}</li> | |
<li>email: {{ user.email }}</li> | |
</ul> | |
</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
import { db } from '~/plugins/firebase.js' | |
import { mapGetters } from 'vuex' | |
export default { | |
created: function () { | |
this.$store.dispatch('setUsersRef', db.collection('users')) | |
}, | |
computed: { | |
...mapGetters({ users: 'getUsers' }) | |
}, | |
} | |
</script> | |
<style> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment