๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ/WEB

Vue ์‹œ์ž‘ํ•˜๊ธฐ (2) | Firebase ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ, FireStore๊ถŒํ•œ ์ „์ฒดํ—ˆ์šฉ, Vue์—์„œ FireStore๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

๋ฐ˜์‘ํ˜•

 

๐Ÿ“„ ๋ชฉ์ฐจ

     

    1ํŽธ์—์„œ ๋งŒ๋“ค Form input์„ ์ €์žฅ์†Œ์™€ ์—ฐ๊ฒฐํ•ด์•ผ ํ•˜๋Š”๋ฐ์š”,

    ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ €์žฅ์†Œ๋ฅผ ๋งŒ๋“ค์–ด vue์™€ ์—ฐ๊ฒฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

    ์„œ๋ฒ„๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค๊ธฐ๋Š” ๋ฒˆ๊ฑฐ๋กœ์šฐ๋‹ˆ Firebase๋ฅผ ํ™œ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    1. Firebase ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

    ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  google analytics๊ณ„์ •์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

    Firebase ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ 1๋‹จ๊ณ„
    Firebase ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ 2๋‹จ๊ณ„
    Firebase ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ 3๋‹จ๊ณ„

    ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด ์ฝ˜์†”๋กœ ์ด๋™ํ•˜๋Š”๋ฐ์š”,

    ์•ฑ ์ถ”๊ฐ€ > Web์„ ์„ ํƒํ•˜๋ฉด, npm๋ฐฉ์‹์œผ๋กœ web์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ apikey๋“ฑ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

    ์ด key๊ฐ’๋“ค์„ ์ž˜ ๋ณต์‚ฌํ•ด๋‘์„ธ์š”. vue์ฝ”๋“œ ์•ˆ์— ์‚ฝ์ž…ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. 

     

    2. Firebase์— ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

    Firebase์˜ ๋ฐ์ดํ„ฐ๋ฅผ vueํ”„๋กœ์ ํŠธ์™€ ์—ฐ๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋งŒ๋“ค๊ธฐ๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

     

     

    ๊ทธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ˆ์— ์ƒˆ๋กœ์šด ์ปฌ๋ž™์…˜์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

    '์ปฌ๋ž™์…˜ ์‹œ์ž‘'์„ ๋ˆ„๋ฅด๋ฉด ์ƒˆ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์ด๋ฆ„์€ cities๋กœ ํ•˜๊ณ  seoul์ด๋ผ๋Š” ์•„์ดํ…œ์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    3. Firebase์— ๋ฐ์ดํ„ฐ ๊ถŒํ•œ ๋ณ€๊ฒฝ

    ๋ฐ์ดํ„ฐ ์˜†์˜ '๊ทœ์น™' ํŒจ๋„์— ๊ฐ€์„œ ๊ถŒํ•œ ๊ทœ์น™์„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

    ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ „์ฒด ํ—ˆ์šฉํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }

     

    4. Vue์™€ Firebase  ์—ฐ๊ฒฐ 

    ์•„๋ž˜ ๋ช…๋ น์œผ๋กœ firebase ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ vue์— ์ธ์Šคํ†จํ•˜๊ณ 

    > npm install firebase

     

    plugins/firebase.ts์—์„œ Firebase ๊ด€๋ จ ํ•จ์ˆ˜์™€ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. 

    // plugins/firebase.ts
    import Vue from 'vue'
    import * as firebase from "firebase/app";
    import 'firebase/firestore'
    import { getFirestore, collection, addDoc, getDocs} from "firebase/firestore";
    
    
    // Initialize Firebase
    const firebaseConfig = {
      ..
    };
    firebase.initializeApp(firebaseConfig);
    
    const db = getFirestore();
    
    // report ์ปฌ๋ž™์…˜ ์ฝ๊ธฐ
    const readReportList = async() => {
      const querySnapshot = await getDocs(collection(db, "report"));
      return querySnapshot
    }
    
    // report ์ปฌ๋ž™์…˜์— ์“ฐ๊ธฐ
    const addToReport = async(data) => {
      await addDoc(collection(db, "report"), data);
    }
    
    export {addToReport, readReportList};

     

    main.ts์—์„œ plugins/firebase.ts๋ฅผ ์ž„ํฌํŠธํ•ด์ค๋‹ˆ๋‹ค

    import Vue from 'vue'
    import App from './App.vue'
    import './registerServiceWorker'
    import router from './router'
    import store from './store'
    import vuetify from './plugins/vuetify'
    import './plugins/firebase'
    
    Vue.config.productionTip = false
    
    new Vue({
      router,
      store,
      vuetify,
      render: h => h(App)
    }).$mount('#app')

     

    5. Vue์—์„œ Firebase๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

    (1)์—์„œ ๋งŒ๋“  form ์— firebase db๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.

    <template>
    	<div>
    		<v-card
    		elevation="2"
        style="width: 500px; margin: 20px auto; padding: 20px;"
    	>
    		<v-form
    		ref="form"
    		lazy-validation
    		>
    		<v-text-field
    			v-model="report.name"
    			:counter="10"
    			label="์ด๋ฆ„"
    			required
    		></v-text-field>
    		<v-text-field
    			v-model="report.completed"
    			label="์™„๋ฃŒ"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.declined"
    			label="์ˆ˜์ทจ๊ฑฐ๋ถ€"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.fresh"
    			label="ํ”„๋ ˆ์‹œ๋ฐฑ"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.notScanned"
    			label="๋ฏธ์Šค์บ”"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.incomplete"
    			label="๋ฏธ๋ฐฐ์†ก"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.canceled"
    			label="์ทจ์†Œ๊ฑด"
    			type="number"
    		/>
    		<v-text-field
    			v-model="report.retrieval"
    			label="ํšŒ์ˆ˜"
    			type="number"
    		/>
    		</v-form>
        	<v-btn 
    			elevation="2"
    			v-on:click= "submitReport">์ œ์ถœ</v-btn>
        	</v-card>
    		<li v-for="(report, idx) in reportedList" :key="idx">
    			{{ report }}
    		</li>
    	</div>
    	
    </template>
    
    <script lang="ts">
      import Vue from 'vue'
      import {addToReport, readReportList} from '../plugins/firebase'
      // import HelloWorld from '../components/HelloWorld.vue'
    
      export default Vue.extend({
        name: 'Home',
    	data() {
    		return {
    			report: {
    				name: '',
    				completed: 10,
    				declined: 0,
    				fresh: 0,
    				notScanned: 0,
    				incomplete: 0,
    				canceled: 0,
    				retrieval: 0
    			},
    			reportedList : [] as  any
    		}
    	},
    
        components: {
          // HelloWorld,
        },
    
    	methods: {
    		submitReport(){
    			addToReport(this.report)
    			console.log(this.report)
    		},
    		
    	},
    
    	async created() {
    		const reportList = await readReportList();
    		const result = reportList.docs.map((doc) => doc.data());
    		console.log(result);
    		this.reportedList = result
    		
    	}
      })
    </script>

     

     

    ํผ ์ž…๋ ฅ์ฐฝ์„ ์ฑ„์šฐ๊ณ  submit์„ ๋ˆ„๋ฅด๋ฉด firebase์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. 

     

    ๋ฐ˜์‘ํ˜•