본문 바로가기

카테고리 없음

VueJS 에서 component 합치기

728x90
반응형

 

 

 

라우터로 경로를 지정 후

 

( 아래 참고 )

 

soobindeveloper8.tistory.com/249

 

 

 

VueJS 의 index.js

VueJS 의 router 폴더의 index.js 에서는 router 를 지정해준다. 나는 아래와 같이 구성해 놓았다 import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' Vue.use(Vue..

soobindeveloper8.tistory.com

 


HOME 의 구성요소

 

'/'  를 클릭시에  ../views/Home.vue  이 파일로 이동하는데  

../views/Home.vue 파일은 아래와 같다

<template>
  <div class="home">
    <div class="side_menu">
      <SideBar></SideBar>
    </div>
    <div class="lucy_body">
      <Title></Title>
      <SearchCondition/>
      <Btn/>
      <BoardList msg="Welcome to Your Vue.js App"/>
      <Pageing/>
      <popupLayer/>
    </div>
</div>

</template>

<script>
// @ is an alias to /src
import BoardList from '@/components/Home/BoardList.vue'
import Btn from '@/components/Home/Btn.vue'
import SearchCondition from '@/components/Home/SearchCondition.vue'
import Pageing from '@/components/Home/Pageing.vue'
import Title from '@/components/Home/Title.vue'
import SideBar from '@/components/sideMenu/Home_Sidemenu.vue'
import popupLayer from '@/components/Home/popupLayer.vue'

export default {

  name: 'Home',
  components: {
    BoardList,
    Btn,
    SearchCondition,
    Pageing,
    Title,
    SideBar,
    popupLayer
  },
  methods: {
  }
}
</script>

<style scoped>
.home{
  height: 800px;
}
.side_menu{
  float: left;
  width: 13%;
}
.lucy_body{
  float: right;
  width: 85%;
  margin-right: 15px;
}
</style>

 

Home 은 아래처럼  7개의 컴포넌트 로 구성이 되어있다   

 

 


상단바 하단바 고정

 

일단 상단바와 하단바는

App.vue 에서 항상 고정 값이기 때문에 아래처럼 설정을 했다.

 

 


router-link

상단바와 사이드바에는 각기 router-link 가 연결 되어 있다.

( <a></a> 태그와 비슷한 역활 )

 

 

 

 


router-view

router-link 를 클릭하게 되면  router-link 가 가지고 있는 vue를 router-view 가 보여주게 된다.

 

그렇기 때문에 이 부분만 유동적으로 변할 수 있는 것이다.

 

 


components 구성

router-link 가 가르키고 있던 vue 파일 중에 '/' home 파일을 한번 들여다보자.

 

위에 코드 처럼 import 한 vue 파일이 각기 위치에 배치 된다.

 

각 vue 파일은 별개로 구성되어 있지만 Home vue 에서 하나의 vue 로 통합되었다.

 


componenet 더 자세히

 

BoardList 를 살펴보자.

 

vue  는 늘 <template> HTML </template> 로 감싸고 있어야 한다

 

<template>
    <table class="divTableBody">
      <colgroup>
        <col width="40px">
        <col width="18%">
        <col>
        <col width="7%">
        <col width="120px">
      </colgroup>
        <th class="header_th"><input type="checkbox" class="check_box"></th>
        <th class="header_th">채널</th>
        <th class="header_th">문구</th>
        <th class="header_th">사용여부</th>
        <th class="header_th">등록일</th>
      <tr class="divTableRow body_tr" v-for="vo in boardList" v-bind:key="vo.blog">
        <td class="divTableCell"><input type="checkbox" class="check_box"></td>
        <td class="divTableCell">{{vo.channel}}</td>
        <td class="divTableCell">{{vo.phrase}}</td>
        <td class="divTableCell">{{vo.usagestatus}}</td>
        <td class="divTableCell">{{vo.registrationDate}}</td>
      </tr>
  </table>
</template>

<script>
const axios = require('axios')

export default {
  el: '.divTableBody',
  data: function () {
    return {
      boardList: []
    }
  },
  created () {
    axios.get('/data?msg=123')
      .then(res => {
        this.boardList = res.data.boardList
      })
  },
  mounted () {
    console.log('Child mounted')
  },
  methods: {
    popupCreate () {
      alert('aaa')
    }
  }
}
</script>

<style scoped>
@import '../../assets/css/Home/Home.css'
</style>

 


css 파일을 따로 만들어서 impor 한 방법이다.


 

 

 

옆에 방법은 Controller 와 연동해서

 

지금 시도하고 있는 방법인데

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/data 는 Cotroller 매핑 주소이고

 

msg에 123 을 실어서 보낸다는 의미이다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

성공을 하면

DATA 를 받아와서

 

then 을 실행 시키는데

 

 

 

 

 

 

 

 

 

 

 

 

 

 

받아온 데이터를 

 

내 boardList 에 실어주는 과정이다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

V-FOR 문을 이용하여서 boardList 를  보여주게 된다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

이게 기본키 같은 고유한 키라는데 이게 없으면 에러가 발생한다.

이유는 자세히는 모르겠다

 

 

이러한 과정을 거쳐서 하나의 component 를 완성한다.

이렇게 완성시킨 component를 Home.vue 에서 가져다 쓰는 것이다.

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

( 1 ) BoradList.vue 에서 완성시킨 templet을 Home.vue 에서 iport 한다

 

( 2 ) components 에 등록을 해준다. 여기서 등록을 하지 않으면 사용할 수 없다.

 

( 3 ) Home.vue 의 template 안에 component를 등록해줌으로써 끝

 

 

 


위 와 같은 방법으로 여러개의 컴포넌트를 생성 한 뒤 하나의 템플릿을 완성시키고

 

이런식으로 생성된 템플릿들이 모여서 

 

상위의 컴포넌트가 되어준다.

 

상위에서는 그러한 컴포넌트를 모아서 또다른 템플릿을 만들고 

 

이런 세분화 된 계층 구조의 연속인 것 같다.

728x90
반응형