Skip to content

Instantly share code, notes, and snippets.

@wataruoguchi
Last active November 9, 2019 01:42
Show Gist options
  • Save wataruoguchi/2bdc234bc2f91b1b28f84b959d8064f3 to your computer and use it in GitHub Desktop.
Save wataruoguchi/2bdc234bc2f91b1b28f84b959d8064f3 to your computer and use it in GitHub Desktop.
VuexFireでNuxt.jsアプリに一瞬でFirestoreを導入する ref: https://qiita.com/wataruoguchi/items/8fdda8e9754658be06be
<proj>
L src
L plugins
L components
L package.json
L ...
L functions
L public
<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>
<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>
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 }
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
module.exports = {
...
plugins: ['~/plugins/firebase'],
...
}
{
"apiKey": "<API_KEY>",
"authDomain": "<PROJECT_ID>.firebaseapp.com",
"databaseURL": "https://<DATABASE_NAME>.firebaseio.com",
"projectId": "<PROJECT_ID>",
"storageBucket": "<BUCKET>.appspot.com",
"messagingSenderId": "<SENDER_ID>"
}
<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