æ ªå¼ä¼ç¤¾ROLOããã主å¬ãã¦ããJavaScript Buildersã§åããHands-onã§ãã£ããã¨ã復ç¿ãå ¼ãã¦èª¿ã¹ãªããã¡ã¢ã
1. Nuxtã®ããã¸ã§ã¯ããä½æ
$ npx create-nuxt-app <Project Name> $ cd <Project Name>
create-nuxt-app
ãã¤ã³ã¹ãã¼ã«ããè¦ãããªãã®ã§ãããnpx
ã³ãã³ãã ã¨ãã®ã¾ã¾ããã¸ã§ã¯ããä½æã§ããã£ã½ã!?
Nuxt.jsã®èµ·å
$ yarn run dev
ãã©ã¦ã¶ã§http://localhost:3000
ã«ã¢ã¯ã»ã¹
Nuxt.jsã§éçãã¡ã¤ã«ã®çæ
$ yarn run generate
2. ããã¸ã§ã¯ããFirebaseã«æ¥ç¶ãã
Firebaseã®ã³ã³ã½ã¼ã«ããæ°è¦ããã¸ã§ã¯ããä½æãã¦ããåæã
2-1. Firebase CLIã®ã¤ã³ã¹ãã¼ã«
Firebase CLIã¯ãFirebase ããã¸ã§ã¯ãã®ç®¡çã表示ããããã¤ãè¡ãããã®ãã¾ãã¾ãªãã¼ã«ãæä¾ãã¾ãã
firebaseã使ãããã¸ã§ã¯ããä»å¾ãã¤ãããããªãglobalã«ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ã£ã¦è¯ãã¨æãã¾ãã
$ npm install -g firebase-tools
2-2. Firebaseã¢ã«ã¦ã³ãã«PCãæ¥ç¶
$ firebase login
ã³ãã³ããå®è¡ããã¨googleã¢ã«ã¦ã³ãã®é¸æç»é¢ã表示ãããã®ã§ãFirebaseã使ç¨ãã¦ããã¢ã«ã¦ã³ããé¸æãã¦Firebase CLIã«è¨±å¯ãä¸ãã
2-3. Firebaseã®è¨å®ãããã¸ã§ã¯ãã«è¿½å
Nuxtã§Firebaseã使ãããã®ã§ã1.ã®create-nuxt-app
ã§ä½æããNuxtããã¸ã§ã¯ããã£ã¬ã¯ããªã®ãããã§ä¸è¨ã³ãã³ããå®è¡
$ firebase init
firebase init ã³ãã³ãã§ã¯ãæ°è¦ãã£ã¬ã¯ããªã¯ä½æããã¾ãããæ°ããããã¸ã§ã¯ããã¼ãããéå§ããå ´åã¯ãå ã«ãã£ã¬ã¯ããªãä½æãããã®æ°ãããã£ã¬ã¯ããªã«ç§»åãã¦ãããinit ã³ãã³ããå®è¡ããå¿ è¦ãããã¾ãã
åºå ¸: Firebase CLI リファレンス | Firebase
ã³ãã³ããå®è¡ããã¨ã¿ã¼ããã«ä¸ã§è²ã ã¨è¨ããã¾ãã
? Which Firebase CLI features do you want to setup for this folder? Press Space to sele ct features, then Enter to confirm your choices.
ã©ã®ãµã¼ãã¹ä½¿ãã
ä¸ä¸ãã¿ã³ã§ä½¿ããããµã¼ãã¹ã«ç§»åãã¦ã¹ãã¼ã¹ãã¼ã§é¸æ â» æ¥æ¬èªå
¥åã«ãªã£ã¦ããã¨ã¹ãã¼ã¹ã§é¸æã§ããªãã®ã§æ³¨æ
ãã¼ã¿ãã¼ã¹(firestore)ã¨ãã¹ãã£ã³ã°(Hosting)ãé¸æ
? Select a default Firebase project for this directory
ã©ã®Firebaseããã¸ã§ã¯ããå©ç¨ããã
äºãä½ã£ã¦ãããããã¸ã§ã¯ããé¸æ
? What file should be used for Firestore Rules? (firestore.rules)
ãã¼ã¿ãã¼ã¹(firestore)ã®ã«ã¼ã«ãã¡ã¤ã«åã¯ä½ã«ããã
? What file should be used for Firestore indexes? (firestore.indexes.json)
ãã¼ã¿ãã¼ã¹(firestore)ã®ã¤ã³ããã¯ã¹ã®ãã¡ã¤ã«å (ã¤ã³ããã¯ã¹ãã¡ã¤ã«ã¯ã¡ãã£ã¨ç解ã§ãã¦ãªã
? What do you want to use as your public directory? (public)
Firebaseã®Hosthingã«ã¢ãããã¦å
¬éãããã£ã¬ã¯ããª
Nuxtã®å ´åã¯éçãã¡ã¤ã«ã®çæãdist
ãã£ã¬ã¯ããªã«ãªãã®ã§dist
ã¨å
¥å
? Configure as a single-page app (rewrite all urls to /index.html)? (y/N)
SPAæ§ã«è¨å®ããã
質åãçµããã¨Firebaseé¢é£ã®ãã¡ã¤ã«ãçæããã
.firebaserc
firebase.json
firestore.indexes.json
firestore.rules
dist/index.html
2.4. Firebaseã«ãããã¤
ä¸è¨ã³ãã³ãã§ããããã¤ãããã£ã¬ã¯ããªã«æå®ãã./dist
å
ãã¢ãããã¼ãããã
$ firebase deploy
3. Firebaseã®æ¥ç¶æ
å ±ã.env
ãã¡ã¤ã«ããèªã¿è¾¼ããããã«ãã
Nuxt.js ã®
dotenv-module
ãå©ç¨ããäºã§ããããã£ãç°å¢åå¥ã®è¨å®æ å ±ã.env
ãã¡ã¤ã«ãç¨ãã¦æè»ã«åãæ¿ãããã¨ãåºæ¥ãããã«ãªãã¾ãã
.env
å ã®å¤æ°æ å ±ã¯ãNuxt å ã«ã¦ãprocess.env.API_URL
ã®ããã«ãã¦åç §ããäºãå¯è½ã§ãã
åºå ¸: Nuxt.js で dotenv を活用する – chatbox.blog
@nuxtjs/dotenvã®ã¤ã³ã¹ãã¼ã«
$ yarn add @nuxtjs/dotenv
3-1. Nuxtã§dotenvã¢ã¸ã¥ã¼ã«ã®ä½¿ç¨ãè¨å®
nuxt.config.jsã®modules
ã«è¨å®ã追å
module.exports = { /* ** Nuxt.js modules */ modules: [ '@nuxtjs/dotenv', ], }
3-2. .env
ãã¡ã¤ã«ã«firebaseã®æ¥ç¶æ
å ±ãè¨è¿°
.env
ãã¡ã¤ã«ã®ä½æ
$ touch .env
firebaseããã¸ã§ã¯ãã®è¨å®ãã</>
ã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦è¡¨ç¤ºãããæ¥ç¶æ
å ±ã.env
ãã¡ã¤ã«ã«è¨å®ãã¾ãã
.env
ãã¡ã¤ã«ã¯Nuxtã®ããã¸ã§ã¯ãã§ã¯èªåçã«.gitignore
ããã¦ããã®ã§ééã£ã¦git pushãã¦ãã¾ãå¿é
ãç¡ãããã§ãï¼
</>
ãã¯ãªãã¯ããã¨æ¥ç¶æ
å ±ãã¢ã¼ãã«ã§è¡¨ç¤ºããã
FB_API_KEY=<FIREBASE_API_KEY> FB_AUTH_DOMAIN=<FIREBASE_ AUTH_DOMAIN> FB_DATABASE_URL=<FIREBASE_DATABASE_URL> FB_PROJECTID=<FIREBASE_PROJECT_ID> FB_STORAGE_BUCKET=<FIREBASE_ STORAGE_BUCKET> FB_MESSAGING_SENDER_ID=<MESSAGING_SENDER_ID>
3-3. Nuxtã§firebaseã®æ¥ç¶ããã
plugins
ãã£ã¬ã¯ããªã«Firebaseã¨æ¥ç¶ããããã®ãã©ã°ã¤ã³ãä½æãã
Nuxtã§ã¯(Vueã§ã¯ï¼)èªåã®ã©ã¤ãã©ãªã¯plugins
ãã£ã¬ã¯ããªå
ã«ç½®ããä½æ³ã£ã½ãã§ãã
plugin/firebase.js
import firebase from "firebase"; // .env ã«è¨å®ããå¤ãåå¾ãã¦ã const config = { apiKey: process.env.FB_API_KEY, authDomain: process.env.FB_AUTH_DOMAIN, databaseURL: process.env.FB_DATABASE_URL, projectId: process.env.FB_PROJECTID, storageBucket: process.env.FB_STORAGE_BUCKET, messagingSenderId: process.env.FB_MESSAGING_SENDER_ID } if (!firebase.apps.length) { firebase.initializeApp(config) } export default firebase
nuxt.config.js
ã«ãã©ã°ã¤ã³ã®ãã¹ã追å
nuxt.config.js
module.exports = { /* ** Plugins to load before mounting the App */ plugins: [ '~/plugins/firebase', ], }
Firebaseã使ç¨ãããã¼ã¸ã®ãã³ãã¬ã¼ãã§ãã©ã°ã¤ã³ãèªã¿è¾¼ã¿
pages/index.vue
<script> import firebase from "~/plugins/firebase.js" </script>
nuxt.config.js
ã«ãã©ã°ã¤ã³ã®ãã¹ãæ¸ãã°èªåçã«å
¨ãã¼ã¸ã§ä½¿ããããã«ãªã訳ã§ã¯ãªãã
ãã©ã°ã¤ã³ã使ç¨ããããã¼ã¸ã§é½åº¦import
ããå¿
è¦ãããããã§ãã
nuxt.config.js
ã«ãã¹ãæ¸ãã®ã¯éçåãããããæã«ä½ãbundlãããã£ã¦ã®ãwebpackã¿ããã«æ示åãã¦ãã£ã¦æããªã®ã§ããããï¼(ç解ãããã¦ãªãé¨åã§ãã
fc. プラグイン - Nuxt.js
4. Cloud Firestoreã®ãã¼ã¿ã«ã¢ã¯ã»ã¹
ãã¼ã¸ããFirebaseã®ãªã¢ã«ã¿ã¤ã ãã¼ã¿ãã¼ã¹Cloud Firestoreã«æ¥ç¶ãã¦ãã¼ã¿/追å ãåå¾ãã¾ã
pages/index.vue
<script> import firebase from "~/plugins/firebase.js" const db = firebase.firestore(); const settings = { timestampsInSnapshots: true }; db.settings(settings); // ãã¼ã¿ã®åå¾(å ¨ä»¶) const getItems = async () => { return db.collection('<collection_name>') .orderBy('createdAt', 'asc') .get(); }; // ãã¼ã¿ã®è¿½å const addItem = async (content) => { return db.collection('<collection_name>').add({ ...content, createdAt: new Date(), // createAt ãã£ã¼ã«ãããªãã¨ã¨ã©ã¼ã表示ãããã£ã½ãã }) .then((res) => { console.log('Add Document with ID:', res.id); return res.id; }) .catch((err) => { console.error('Error: Add Document', err); throw err; }); }; </script>
ããã§Firebaseã¨ã®é£æºã¯ã§ãã¦ããã®ã§ããã¼ã¿ã®åå¾ã»è¿½å ã¡ã½ãããé©æå®è¡ããããã«ããã°OK
yarn run dev
ãã¦ããã°ããããªãã¼ãã§ä½ã£ã¦ããæä¸ã®ãã®ã確èªã§ãã¾ãã
Cloud Firestoreã使ã£ãã¢ããªãå®æãããä¸è¨ã³ãã³ãã§éçãã¡ã¤ã«ãçæãã¦ãFirebaseã«ãããã¤ã
$ yarn run generate $ firebase deploy
ã¾ã¨ãã¨ææ³
æ¬å½ã«ç°¡åã«Firebaseã¨é£æºãããã®ãä½ãå
¬éã¾ã§ãããã¨ãã§ãã¾ããï¼ï¼(2,3æéç¨åº¦ã§)
æ°ãããã¨ãåºæ¥ãããã«ãªãã£ã¦ããã¼ã®ãã¼ï¼
è²ã
ãªãµã¼ãã¹ã使ã£ã¦ç°¡åã«åããããåããã®ãä½ããã£ã¦ã®ã¯ã¢ããã¼ã·ã§ã³ã®ç¶æã«ãã¨ã¦ãè¯ãã§ããã
ã¨ãåæã«è¨å®ãã¡ã¤ã«ã®ãã¨ããNuxtãã¬ã¼ã ã¯ã¼ã¯ã®ãä½æ³ã¨ããã¾ã ã¾ã ç解ã§ãã¦ãªãé¨åãããç¹ã«Vue.jsãç解ãã¦ãªãã¨Nuxtã®ãä½æ³ã®ç解ã¯é£ãããã ãªã¼ã£ã¦é¨åã¨éçåãããããã¡ã¤ã«ã®ä¸ãè¦ã¦ããããã©ãã¯ããã¯ã¹ãªã¾ã¾ã§ãã©ããã£ã¦éçãªHTML+CSS+JavaScriptããFirebaseã«æ¥ç¶ãã¦ããã®ãã£ã¦é¨åãç解ããªããã°ã¨æ°ãã課é¡ãè¦ã¤ããã¾ããã
ç´ æ´ãããHands-onãéå¬ãã¦ããã¦æ¬å½ã«ãããããã
æ å ±æ ¼å·®ã«ã¤ãã¦æãããã¨ã®ãã¨ã ï¼æ¸ãã¦ãããã¡ã«é·ããªã£ãã®ã§èªãå¿ è¦ãªãï¼
æ±äº¬ ç¡æã®åå¼·ä¼ãHands-onããã£ã¡ãå¤ãã¦ãã¸ã¤ã´ã¡ã¤ã
ã¤ã³ã¿ã¼ããããæ¬ã ããããªãããªã¢ã«ã«è§¦ãããæ
å ±ã¸ã®ã¢ã¯ã»ã¹ã®å®¹æãã身è¿ã«ç´ã«å°ãããã¨ãåºæ¥ã人ãå¤ãã£ã¦ç°å¢ã®æ ¼å·®ãããã¨ãããç°èã¡ã»ã¼ã¨å·®ãçãããã£ãªã¼ã£ã¦ã»ã¼éçéè½ã®ç°èã¡ã»ã¼éãã«ãªã£ã¦ããã³ã¨ãã¦ã¯å®æãã¦ãæ¥ã
ã§ãããã³ããããããéãã§ãã
ãããã¨ãconnpassï¼ (Doorkeeperã®ã¤ãã³ãã¯ææã®ãå¤ãèæ
ã¤ã³ã¿ã¼ããããSNSã§æ
å ±å¾ãããããï¼ã£ã¦ãæè¦ããããã¨æãã¾ãããã¤ã³ã¿ã¼ãããã¯è½åçã«æ¤ç´¢ããªããã°æ
å ±ã¯å¾ããããæ¢ãã¦ããæ
å ±ä»¥å¤ã¯å¾ã«ããã¨æãã¦ãã¾ãã
ããã¦æ¤ç´¢ãã¦åºã¦ããæ
å ±ãå®éã«ä½¿ã£ã¦ã¿ã¦ã©ããªã®ãï¼ã¨ãããæ¹ã沢山åºã¦ãããããªã¸ã£ã³ã«ã§ã¯ã©ãããææ³ã§ã©ã®ããæ¹ãé¸æããã®ãã¹ãã¼ããªã®ãï¼ã¨ãã£ãäºãå¤æããã®ãç¹ã«ç¥èã®ä¹ããã¸ã£ã³ã«ãå¦ã³å§ãã人ã«ã¨ã£ã¦ã¯é£ããã¨æãã®ã§ãã
æ
ã«èº«è¿ã«å®éã«ä½¿ã£ã¦ãã人ãå¤ãå±
ã¦ããã®äººéã®æè¦ãèããå ´ãå¤ãã¨ããç°å¢ã¯ãæ¤ç´¢ã§ã¯ã§ã§æ¥ãªãã£ãå¥ã®ã¢ããã¼ããªã©äºæãã¬ç¥èãå¾ãããå ´ã«ããªã£ã¦ããã¨ããæå³ã§ãã¤ã³ã¿ã¼ããããSNSã ãã®ç°å¢ããåªä½ã ã¨æãã¦ãã¾ãã
ã¤ã¾ããæ¬å±ããªãå°åã§ãã¢ãã¾ã³ãããã°æ¬ã¯è²·ãããã©ãæ¬å±ãã¶ãã¤ãã¦å¶ç¶ç®ã«å ¥ã£ãã¢ããç«ã¡èªã¿ãã¦è¯ãã£ãããè²·ã£ããã¨ãè£ ä¸ã綺éºãªæ¬ã ã£ãããã¸ã£ã±è²·ããããã¨ãããããªå¶ç¶ã®åºé¢ãã¯å°é£ãã¨ãããããªæè¦ã§ãã
ã¢ãã®æ代ãããéèã®æ代ã«ãªããæ
å ±ã®æ代ãçµã¦ãç¥èã®æ代ã«ãªãã®ã§ããã°ãç¥èã«è§¦ããããæ©ä¼ãç¨æãªå ´æã®æ¹ãåªä½ã«ãªãã®ã§ã¯ãªãããã¨ããæè¦ã
ãéã»ç
åèã»éãããæªæ¥ã«å
åãããæèæ³ãã¨ãã«å½±é¿ãåãã¦ããã®ã§ãããªæè¦ãªã®ã ã¨æãããã©ã
[åè]
- Nuxt.js - The Vue.js Framework
- 【Nuxt.js】create-nuxt-appでNuxt.js(v2.0.0)を始める - Qiita
- 【v2対応】Nuxt.jsとFirebaseを組み合わせて爆速でWebアプリケーションを構築する - Qiita
- Firebaseの始め方 - Qiita
- Firebase を JavaScript プロジェクトに追加する | Firebase
- Firestore の Timestamp の仕様変更による警告と、その対処 - Qiita
- ä½è : è±è°·æ磨
- åºç社/ã¡ã¼ã«ã¼: ã·ã¼ã¢ã³ãã¢ã¼ã«ç 究æ
- çºå£²æ¥: 2018/10/17
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã°ãè¦ã