Skip to content

Commit a19b2a8

Browse files
committed
Init Vue Components
Vue.js 컴포넌트에 대한 노트 작성 완료.
1 parent fb0990a commit a19b2a8

File tree

4 files changed

+189
-20
lines changed

4 files changed

+189
-20
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ Vue.js 에 대하여 세부적인 기능을 공부하기 위해 읽었다면 조
1414

1515
0. MVC, MV*, MVVM
1616

17-
1. Vue.js 시작하기
17+
1. [Vue.js 시작하기](https://github.com/tails5555/vuejs_tutorial/blob/master/docs/note_01.md)
1818

19-
2. vue 파일 구성 요소
19+
2. [Vue 파일 구성 요소](https://github.com/tails5555/vuejs_tutorial/blob/master/docs/note_02.md)
2020

21-
3. Vue Instance
21+
3. [Vue Instance](https://github.com/tails5555/vuejs_tutorial/blob/master/docs/note_03.md)
2222

23-
4. Instance Life Cycle
23+
4. [Vue Component](https://github.com/tails5555/vuejs_tutorial/blob/master/docs/note_04.md)
24+
25+
5. Component 의 상하 관계 및 Props
2426

2527
계속 이어집니다.

docs/note_04.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Vue Components
2+
3+
지난 스터디 노트에서 다뤘던 Instance 는 `index.html` 에 사용자가 등록하고 싶은 DOM 객체를 올려서 브라우저에 렌더링 하였습니다.
4+
5+
그렇지만 Web Application 에서는 User Interface 조각 중에 일부는 재활용을 위한 DOM 객체가 존재합니다.
6+
7+
그래서 Vue.js 에서도 재활용이 가능한 형태로 관리하는 주체인 컴포넌트가 있습니다. 물론 컴포넌트를 사용하기 앞서 **인스턴스 초기화** 는 당연히 해둬야 합니다.
8+
9+
이번 노트에서는 Vue 에서 쓰이는 Component 의 개념을 자세히 알아 보겠습니다.
10+
11+
## Creation of Vue Components
12+
13+
Vue.js 에서 컴포넌트를 만드는 과정은 매우 간단합니다.
14+
15+
지난 시간에 다뤘던 `.vue` 파일을 이용하는 방법, 자바스크립트 파일에 컴포넌트를 생성하여 만드는 방법이 있습니다.
16+
17+
1. JavaScript 문장을 이용한 컴포넌트 생성 방법 - 전역 컴포넌트
18+
19+
```javascript
20+
Vue.component('own-component', {
21+
template : `
22+
<h1>{{ message }}</h1>
23+
`,
24+
data : () => ({
25+
message : '안녕하십니까?'
26+
})
27+
});
28+
29+
new Vue({
30+
el: '#component0'
31+
});
32+
33+
new Vue({
34+
el: '#component0_1'
35+
});
36+
```
37+
38+
main.js
39+
40+
```html
41+
<div id="component0">
42+
<own-component></own-component>
43+
</div>
44+
<div id="component0_1"></div>
45+
```
46+
47+
index.html
48+
49+
`main.js` 파일 안에 `Vue.component([컴포넌트 이름], { /* 컴포넌트 속성 */ })` 을 작성하면 `own-component` 를 어느 템플릿에서든 DOM 노드로 사용하여 호출할 수 있습니다. 이를 **전역 컴포넌트** 라고 합니다.
50+
51+
그렇지만 컴포넌트를 생성할 때 주의할 점은 인스턴스의 `data` 는 자바스크립트 객체 문장으로 작성하지만, 컴포넌트의 `data`**함수나 화살표 함수** 를 사용해야 합니다.
52+
53+
전역 컴포넌트는 User Interface 에서 Header, Footer 등 항상 필요한 노드에 적용하는 것이 좋습니다.
54+
55+
2. JavaScript 문장을 이용한 컴포넌트 생성 방법 - 지역 컴포넌트
56+
57+
React.js 에서는 컴포넌트를 불러오기 위해선 파일을 직접 생성한 이후에 불러와야 합니다.
58+
59+
그렇지만 Vue.js 에서는 컴포넌트를 렌더링하기 위해 인스턴스에 사용할 컴포넌트를 등록하고 사용해야 합니다.
60+
61+
인스턴스에서 특정 컴포넌트에 대하여 지정을 하고 템플릿 문장에서 DOM 노드 처럼 사용할 수 있게끔 도와주는 컴포넌트를 **지역 컴포넌트** 라고 합니다.
62+
63+
```javascript
64+
const component1 = {
65+
template : '<h1>{{ message }}</h1>',
66+
data : () => ({
67+
message : '안녕!!!'
68+
})
69+
};
70+
71+
const component2 = {
72+
template : '<h1>그래, {{ message }}</h1>',
73+
data : () => ({
74+
message : '안녕!!!'
75+
})
76+
};
77+
78+
new Vue({
79+
el: '#component1',
80+
components: {
81+
'my-component' : component1,
82+
'your-component' : component2
83+
},
84+
template: `
85+
<div>
86+
<my-component></my-component>
87+
<your-component></your-component>
88+
</div>
89+
`
90+
});
91+
```
92+
93+
main.js
94+
95+
또한 Vue.js 에서는 JSX 문법을 사용할 수 있습니다. 하지만 React.js 와 마찬가지로 복수의 컴포넌트를 반환하기 위해 `div`, `li` 등의 태그로 묶어 줘야 합니다.
96+
97+
참고로 Vue.js 에서는 컴포넌트의 이름을 Snake Case 로 짓되, `_` 기호를 `-` 로 사용하는 것이 관례입니다. (예를 들어 `myComponent``my-component` 로 짓는 경우.)
98+
99+
3. `.vue` 파일을 이용한 컴포넌트 생성 방법
100+
101+
지난 시간에도 언급했듯이 `.vue` 파일은 싱글 파일 컴포넌트로, 인스턴스에서 보여줄 컴포넌트에 대하여 지정하고 보여줍니다.
102+
103+
이는 아래를 참고하시길 바랍니다.
104+
105+
- [Vue 파일 구성 요소](https://github.com/tails5555/vuejs_tutorial/blob/master/docs/note_02.md)
106+
107+
## Comparison With React.js Components
108+
109+
1. 전역 / 지역 컴포넌트에 대한 차이
110+
111+
React.js 에서는 컴포넌트에 대하여 전역과 지역에 대한 차이를 따로 두지 않고 모두 **지역 컴포넌트** 만 사용 하였습니다.
112+
113+
그렇지만 Vue.js 에서는 전역 컴포넌트, 지역 컴포넌트에 대한 차이를 두어 User Interface 중에서 공통으로 쓰이는 것들만 묶어 사용하여 매번 지역 컴포넌트를 Importing 하는 것을 줄여주는 것이 그만의 장점으로 보입니다.
114+
115+
하지만 Vue.js 에서는 지역 컴포넌트를 더욱 사용하는 편입니다. 모든 컴포넌트를 전역으로 올리면 Application 성능 뿐만 아니라, 프로퍼티에 대한 참조를 올바르지 못 하게 해석할 수 있는 상황도 발생하기 때문입니다.
116+
117+
2. Fragment 지원 여부
118+
119+
React.js 에서는 복수의 DOM 객체에 대하여 `div`, `li` 등에 대한 태그에 구애 받지 않게끔 사용하는 개념인 `Fragment` 태그가 존재합니다.
120+
121+
그렇지만 Vue.js 에서 `Fragment` 를 사용하기 위해서는 다른 라이브러리를 사용하는 방법 밖에 없습니다.
122+
123+
이를 위한 대표적인 라이브러리가 `vue-fragment` 인데 이는 `npm` 이나 `yarn` 을 이용해서 설치하고 사용하시면 됩니다.
124+
125+
## Author
126+
127+
- 강인성([tails5555](https://github.com/tails5555))

vue_tutorial/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
<title>vue_tutorial</title>
77
</head>
88
<body>
9-
<div id="app"></div>
9+
<div id="app1"></div>
1010
<div id="app2"></div>
1111
<div id="app3"></div>
12-
<div id="app4"></div>
13-
<!-- built files will be auto injected -->
12+
13+
<div id="component0">
14+
<own-component></own-component>
15+
</div>
16+
<div id="component0_1"></div>
17+
<div id="component1"></div>
1418
</body>
1519
</html>

vue_tutorial/src/main.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
// The Vue build version to load with the `import` command
2-
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
31
import Vue from 'vue'
42
import App from './App'
53
import SmallBox from './components/SmallBox'
64
import router from './router'
75

86
Vue.config.productionTip = false
97

10-
/* eslint-disable no-new */
118
new Vue({
12-
el: '#app',
13-
router,
14-
components: { App },
15-
template: '<App/>'
16-
});
17-
18-
new Vue({
19-
el: '#app2',
9+
el: '#app1',
2010
template: '<h1>Hello, {{ message }}</h1>',
2111
data: {
2212
message : 'Vue.js'
2313
}
2414
});
2515

2616
new Vue({
27-
el: '#app3',
17+
el: '#app2',
2818
template: '<button v-on:click="this.handleClick">클릭</button>',
2919
data: {
3020
message : '안녕, Vue.js!'
@@ -37,7 +27,53 @@ new Vue({
3727
});
3828

3929
new Vue({
40-
el: '#app4',
30+
el: '#app3',
4131
components: { SmallBox },
4232
template: '<SmallBox />'
33+
});
34+
35+
Vue.component('own-component', {
36+
template : `
37+
<h1>{{ message }}</h1>
38+
`,
39+
data : () => ({
40+
message : '안녕하십니까?'
41+
})
42+
});
43+
44+
new Vue({
45+
el: '#component0'
46+
});
47+
48+
new Vue({
49+
el: '#component0_1',
50+
template: '<own-component></own-component>'
51+
});
52+
53+
const component1 = {
54+
template : '<h1>{{ message }}</h1>',
55+
data : () => ({
56+
message : '안녕!!!'
57+
})
58+
};
59+
60+
const component2 = {
61+
template : '<h1>그래, {{ message }}</h1>',
62+
data : () => ({
63+
message : '안녕!!!'
64+
})
65+
};
66+
67+
new Vue({
68+
el: '#component1',
69+
components: {
70+
'my-component' : component1,
71+
'your-component' : component2
72+
},
73+
template: `
74+
<div>
75+
<my-component></my-component>
76+
<your-component></your-component>
77+
</div>
78+
`
4379
});

0 commit comments

Comments
 (0)