๐Ÿ‘ฉ‍๐Ÿ’ป/Firebase

[firebase] Vue.js + Element UI + Firebase : ์…‹์—… + ๊ฐ„๋‹จํ•œ ์ •๋ณด ๋“ฑ๋ก

ํ•œ๋‚˜ 2020. 12. 17. 21:35

๊ฐ„๋‹จํ•œ ์ฑ… ์ •๋ณด์™€ ๋ฆฌ๋ทฐ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์›น์•ฑ์„ Vue์™€ Element UI ํ”„๋ ˆ์ž„์›Œํฌ, ๊ทธ๋ฆฌ๊ณ  Firebase๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค๊ณ  ์žˆ๋‹ค.

์ด๊ณณ์„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋” ๊ฒฝ๋กœ์—์„œ node prompt๋ฅผ ์—ด๊ณ  ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

 

vue create [vue-project-name]

 

Vue CLI v4.x ๋ถ€ํ„ฐ๋Š” almost-full ์˜ต์…˜์ด ์ƒ๊ธด ๋ชจ์–‘์ด๋‹ค. ์ด๋ฒˆ์— ํ•˜๋ฉด์„œ ์ฒ˜์Œ ๋ดค๋‹ค. ์†์‰ฝ๊ฒŒ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ๋‹ค ์žˆ์–ด์„œ, ํ•ด๋‹น ์˜ต์…˜์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  element UI ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์จ์คฌ๋‹ค. ๋ฌธ์„œ๋Š” ์ด๊ณณ

npm i element-ui -S

 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์ผ ํ•˜๋ฉด์„œ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ผ๋˜ vue-admin-template์„ ์ด์šฉํ•˜๋ ค๊ณ  ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋Š” ์ง€์šฐ๊ณ , ์ด๊ณณ์—์„œ git clone์„ ํ•ด์™”๋‹ค.

 

# clone the project
git clone https://github.com/PanJiaChen/vue-admin-template.git

# enter the project directory
cd vue-admin-template

# install dependency
npm install

# develop
npm run dev

 

์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์ด ๋ฐ๋ชจ ๋ฒ„์ „๋ณด๋‹ค ๋” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ์„ ์ œ๊ณต ๋ฐ›๋Š”๋‹ค. ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ์‚ด๋ฆฌ๊ณ  ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์€ ์ผ๋‹จ ์ง€์›Œ๋‘์—ˆ๋‹ค.

 

git repo๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ , ๊ธฐ์กด git์„ ์ดˆ๊ธฐํ™”ํ•œ ๋‹ค์Œ repo์— ์˜ฌ๋ ค๋‘” ํ›„, ๋ฆฌ์ŠคํŠธ & ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ ํ™”๋ฉด์„ ๋Œ€๊ฐ• ๋งŒ๋“ค์–ด๋‘” ๋‹ค์Œ, firebase์— ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ–ˆ๋‹ค.

 

firebase.google.com/docs?authuser=0 ์—ฌ๊ธฐ์„œ ์šฐ์ธก ์ƒ๋‹จ '์ฝ˜์†”๋กœ ์ด๋™'ํ•˜๋ฉด ์•„๋ž˜ ๊ฐ™์€ ํ™”๋ฉด์ด ๋œฌ๋‹ค. ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€๋ฅผ ํ†ตํ•ด vue-library-project๋ฅผ ์ƒ์„ฑํ•œ ์ƒํƒœ. ํ”Œ๋žซํผ์€ ์›น์œผ๋กœ ์„ ํƒํ–ˆ๋‹ค.

 

 

Nomad Coders์˜ ํŠธ์œ„ํ„ฐ ํด๋ก  ๊ฐ•์˜์˜ firebase ๋ถ€๋ถ„๋งŒ ์ฐธ๊ณ ํ–ˆ๋‹ค. ๋ฆฌ์•กํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ง„ํ–‰ํ•˜์ง€๋งŒ, vue์™€ ํฌ๊ฒŒ ๋‹ค๋ฅผ ๊ฒŒ ์—†์–ด์„œ ๋„์›€์„ ๋งŽ์ด ๋ฐ›์•˜๋‹ค. Auth ๋ถ€๋ถ„์„ ์šฐ์„  ๋„˜๊ธฐ๊ณ  ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•ด Cloud Firestore & Storage๋งŒ ์šฐ์„  ๊ฑด๋“œ๋ ค๋ดค๋‹ค.

 

 

๊ฐ€์žฅ ์ฒ˜์Œ firebase๋ฅผ ์•ฑ์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

 

ํ”„๋กœ์ ํŠธ ๊ฐœ์š” > ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ์„ค์ • ์•„์ด์ฝ˜ > ํ”„๋กœ์ ํŠธ ์„ค์ •์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค. ๋งจ ์•„๋ž˜ Firebase SDK snippet์ด ์žˆ๋Š”๋ฐ, CDN ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜์ง„ ์•Š์„ ๊ฒƒ์ด๋ผ ๋ฌธ์„œ์— ๋‚˜์˜จ๋Œ€๋กœ

 

npm install --save firebase

// ๋˜๋Š”

yarn add firebase

 

npm ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด package.json์— ์ €์žฅํ•œ๋‹ค.

 

 

์ž˜ ์ €์žฅ์ด ๋˜์—ˆ์œผ๋ฉด, App.vue๊ฐ€ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๊ฒฝ๋กœ์— firebase.js ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ฃผ๊ณ , ์•„๋ž˜์ฒ˜๋Ÿผ import ์‹œ์ผœ์ค€๋‹ค. firestore์™€ storage ๋ถ€๋ถ„์€ ์–ด๋Š ์ •๋„ ์ง„ํ–‰ํ•œ ํ›„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ฒ˜์Œ์—๋Š” import firebase from 'firebase/app'๋งŒ ์žˆ์–ด๋„ ๋œ๋‹ค.

 

config ๊ฐ์ฒด์˜ ๋‚ด์šฉ์€ ์•„๊นŒ์˜ firebase SDK snippet ์—์„œ ๊ธ์–ด์˜ค๋ฉด ๋œ๋‹ค. git์— ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด .env์— ๋”ฐ๋กœ ํ‚ค๋“ค์„ ๋‹ด๊ณ  ์•„๋ž˜์—๋Š” VUE_APP์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ƒ์ˆ˜๋กœ ๋Œ€์ฒดํ–ˆ๋‹ค.

 

 

firebase.initializeApp(firebaseConfig)๋ฅผ ํ†ตํ•ด firebase๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , ํ•„์š”ํ•œ ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์ธ์Šคํ„ด์Šค๋ฅผ export ํ•ด์ค€๋‹ค. ๋งจ ์•„๋ž˜ ๋‘ ์ค„์€ ํ›„์— firestore์™€ storage๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์ง„ํ–‰ํ•œ ํ›„์—ฌ์„œ ์žˆ์ง€๋งŒ ์ฒ˜์Œ์—๋Š” 19๋ฒˆ์งธ ์ค„๋กœ ๋๋‚  ๊ฒƒ์ด๋‹ค.

 

๊ฐ„๋‹จํ•œ form ํ˜•ํƒœ๋ฅผ ์ž‘์„ฑํ•œ bookDetail.vue ๋ผ๋Š” ํŒŒ์ผ์„ ์ค€๋น„ํ•œ๋‹ค. ์ฑ… ์ปค๋ฒ„ ์ด๋ฏธ์ง€์™€ ๋„์„œ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•ด์„œ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” form์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ •๋ณด๋ฅผ ๋ชจ์•„๋‘˜ DB๊ฐ€ ํ•„์š”ํ•˜๋‹ค. Firebase > Cloud Firestore์— ๋“ค์–ด๊ฐ€๋ฉด ์ปฌ๋ ‰์…˜ / ๋‹คํ๋จผํŠธ๋ผ๋Š” ์‹œ์Šคํ…œ์ด ์žˆ๋Š”๋ฐ, bookDetail ์ด๋ผ๋Š” ์ปฌ๋ ‰์…˜์— form์— ์ €์žฅํ•œ ๊ฐ์ฒด๋ฅผ ๋‹ด์„ '๋‹คํ๋จผํŠธ'๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

 

๋‹ค์‹œ bookDetail.vue ์˜ script ๋กœ ์™€์„œ,

 

import { dbService, storageService } from '@/firebase'

dbService๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค.

 

element UI ๋ฅผ ์ด์šฉํ•ด ๊ฐ„๋‹จํ•œ form ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. ์•„์ง ํŒŒ์ผ ์—…๋กœ๋“œ ๊ธฐ๋Šฅ์€ ์—†๋Š” ์ƒํƒœ.

  <el-form ref="form" :model="params" label-width="150px" label-position="left" size="mini" @submit="onSubmit">
      <h3>• ์ฑ… ์ •๋ณด</h3>
      <div class="form-bg">
        <section>
          <el-form-item label="๋„์„œ๋ช…">
            <el-input v-model="params.bookNm" size="small" />
          </el-form-item>
          <el-form-item label="์ €์ž">
            <el-input v-model="params.author" size="small" />
          </el-form-item>
          <el-form-item label="์ถœํŒ์‚ฌ">
            <el-input v-model="params.publisher" size="small" />
          </el-form-item>
        </section>
      </div>
      <el-row type="flex" class="form-btn">
        <el-button type="info" @click="handleCancel">์ทจ์†Œ</el-button>
        <el-button type="primary" @click="onSubmit">๋“ฑ๋ก</el-button>
      </el-row>
    </el-form>

 

import { dbService, storageService } from '@/firebase'

export default {
	name: 'BookDetail', // router/index.js ์— ๋“ฑ๋กํ•œ name๊ณผ ๋™์ผํ•˜๊ฒŒ
    data() {
    	return {
        	list: {}, // object์— ์ฃผ์˜
            params: {
            	bookName: '',
                author: '',
                publisher: ''
            }
        }
    },
    mounted() {
    	// ์ถ”ํ›„์— ์ƒ์„ธ ์ฑ… ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ๋•Œ ์ฝ”๋“œ๋ฅผ ์“ธ ๋ถ€๋ถ„
    },
    methods: {
    	async onSubmit() {
        	event.preventDefault()
            await dbService.collection('bookDetail').add(this.params)
        }
    }
}

์Šคํฌ๋ฆฝํŠธ ๋ถ€๋ถ„์€ ์œ„์™€ ๊ฐ™์€๋ฐ, dbService์— ์•„๊นŒ ์–ธ๊ธ‰ํ•œ collection์ด๋ผ๋Š” ๋Œ€๋ถ„๋ฅ˜ ํด๋”(?) ๊ฐ™์€ ๊ฐœ๋…์— 'bookDetail'์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ params ๊ฐ’๋“ค์„ ์ €์žฅํ•œ๋‹ค.

 

์ด๋Ÿฐ ์‹์œผ๋กœ ์ €์žฅ์ด ๋˜์–ด ๋“ค์–ด๊ฐ„๋‹ค.