Skip to content

Commit 67dcd32

Browse files
authored
Merge pull request #6 from soo-ni/sooni
From sooni To master
2 parents dd165ed + 51293e9 commit 67dcd32

13 files changed

+77
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Web/Front/[Vue.js] 믹스인.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [Vue.js] Mixin
2+
3+
## Mixin이란?
4+
5+
믹스인(Mixins)이란 뷰에서 나온 용어가 아닌 `객체 지향 프로그래밍 언어`에서 다른 클래스의 부모클래스가 되지 않으면서 다른 클래스에서 사용할 수 있는 메서드를 포함하는 `클래스`이다. 구현된 메서드가 포함된 `인터페이스`로 볼 수도 있으며 이 패턴은 SOLID 중 D(dependency inversion)를 적용하는 예이다. 믹스인은 때때로 **상속**이 아니라 **포함**으로 설명된다.
6+
7+
믹스인은 여러 컴포넌트 간에 공통으로 사용하고 있는 로직, 기능들을 **재사용**하는 방법이다. 믹스인에 정의할 수 있는 재사용 로직은 data, methods, created 등과 같은 컴포넌트 옵션이다. 컴포넌트에 mixin을 사용하면 해당 mixin의 모든 옵션이 컴포넌트의 고유 옵션에 **혼합**된다.
8+
9+
* 공식 문서: https://kr.vuejs.org/v2/guide/mixins.html
10+
11+
### Mixin 장점
12+
13+
* 코드 재사용성
14+
* 오버라이딩을 통한 커스텀 및 확장 가능
15+
16+
### 예시
17+
18+
```javascript
19+
// mixin 객체 생성
20+
var myMixin = {
21+
created: function () {
22+
this.hello()
23+
},
24+
methods: {
25+
hello: function () {
26+
console.log('hello from mixin!')
27+
}
28+
}
29+
}
30+
31+
// mixin을 사용할 컴포넌트 정의
32+
var Component = Vue.extend({
33+
mixins: [myMixin]
34+
})
35+
36+
var component = new Component() // => "hello from mixin!"
37+
```
38+
39+
### 옵션 병합
40+
41+
mixin과 컴포넌트 자체에 중첩 옵션이 포함되어 있으면 `병합`된다.
42+
43+
```javascript
44+
var mixin = {
45+
data: function () {
46+
return {
47+
message: 'hello',
48+
foo: 'abc'
49+
}
50+
}
51+
}
52+
53+
new Vue({
54+
mixins: [mixin],
55+
data: function () {
56+
return {
57+
message: 'goodbye',
58+
bar: 'def'
59+
}
60+
},
61+
created: function () {
62+
console.log(this.$data)
63+
// => { message: "goodbye", foo: "abc", bar: "def" }
64+
}
65+
})
66+
```
67+
68+
69+
70+
71+
72+
### :books: 참고
73+
74+
* [Cracking Vue.js - 믹스인](https://joshua1988.github.io/vue-camp/reuse/mixins.html#%EB%AF%B9%EC%8A%A4%EC%9D%B8)
75+
* [Vue.js Mixins: 믹스인은 왜 필요한가? :: 마이구미](https://mygumi.tistory.com/266)
76+
* [[Vue] vue.js Mixin을 활용해서 재사용성을 늘리자!](https://webruden.tistory.com/224)
77+

0 commit comments

Comments
 (0)