Skip to content

Commit e4ed967

Browse files
Merge pull request #19 from kciter/master
Issue #13: 플러그인 사용법 및 예제 페이지 작성
2 parents 6f78a4e + b5f898f commit e4ed967

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
---
2+
layout: post
3+
title: "Vue.js 플러그인 제작 가이드"
4+
date: 2017-01-13 15:53:31 +0900
5+
categories: jekyll update
6+
author: "Lee Sun-Hyoup"
7+
excerpt: "Vue.js에서 플러그인 제작을 예제를 통해 해봅시다."
8+
---
9+
10+
Vue.js는 외부 라이브러리를 쉽게 불러올 수 있도록 표준 플러그인 기능을 제공합니다. `vuex`, `vue-router` 같은 공식적으로 제공해주는 라이브러리도 이 플러그인 기능을 통해 제작되었습니다. 직접 만든 라이브러리를 배포할 수 있도록 플러그인 기능에 대해서 알아봅시다. :)
11+
12+
## 플러그인 사용법
13+
먼저 플러그인은 **제공자****사용자**로 나뉩니다. 이때 두 사이를 이어주는 것이 Vue의 `install` 메서드입니다. 아래는 각각 예제 코드입니다.
14+
15+
### 제공자 코드
16+
```js
17+
// my-plugin.js
18+
MyPlugin.install = function (Vue, options) {
19+
/* ... */
20+
}
21+
```
22+
23+
### 사용자 코드
24+
```js
25+
import MyPlugin from 'my-plugin';
26+
Vue.use(MyPlugin);
27+
```
28+
29+
엄청 간단합니다! `install` 메서드에는 **제공자**가 제공하고자 하는 기능을 작성하면 됩니다. 공식 홈페이지의 예제를 보면,
30+
31+
```js
32+
MyPlugin.install = function (Vue, options) {
33+
Vue.myGlobalMethod = function () {
34+
// 필요한 로직 ...
35+
}
36+
37+
Vue.directive('my-directive', {
38+
bind (el, binding, vnode, oldVnode) {
39+
// 필요한 로직 ...
40+
}
41+
...
42+
})
43+
44+
Vue.mixin({
45+
created: function () {
46+
// 필요한 로직 ...
47+
}
48+
...
49+
})
50+
51+
Vue.prototype.$myMethod = function (options) {
52+
// 필요한 로직 ...
53+
}
54+
}
55+
```
56+
전역 메소드 추가, 전역 디렉티브 추가, 믹스인, 혹은 인스턴스 메소드를 추가할 수 있습니다. Vue에 관련된 것만이 아니라 그 외의 코드도 작성이 가능합니다.
57+
58+
## 실사용 예제
59+
저는 이미 실제 프로젝트에서 몇 가지 플러그인을 제작한적이 있습니다. 아래 예제는 제가 만들었던 플러그인 중 하나인 모달 플러그인입니다.
60+
61+
### 플러그인 vue 파일
62+
```html
63+
<!-- alert-modal-template.vue -->
64+
<template>
65+
<transition name="modal">
66+
<div class="modal-mask" v-if="isShowModal">
67+
<div class="modal-wrapper">
68+
<div class="modal-container">
69+
<div class="modal-header">{{title}}</div>
70+
<div class="modal-body">{{message}}</div>
71+
<div class="modal-footer">
72+
<button class="modal-default-button" @click="close">확인</button>
73+
</div>
74+
</div>
75+
</div>
76+
</div>
77+
</transition>
78+
</template>
79+
80+
<style scoped>
81+
.modal-mask {
82+
position: fixed;
83+
z-index: 9998;
84+
top: 0;
85+
left: 0;
86+
width: 100%;
87+
height: 100%;
88+
background-color: rgba(0, 0, 0, .5);
89+
display: table;
90+
transition: opacity .3s ease;
91+
}
92+
93+
.modal-wrapper {
94+
display: table-cell;
95+
vertical-align: middle;
96+
text-align: center;
97+
}
98+
99+
.modal-container {
100+
width: 300px;
101+
margin: 0px auto;
102+
padding-top: 50px;
103+
background-color: #fff;
104+
border-radius: 2px;
105+
box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
106+
transition: all .3s ease;
107+
font-family: Helvetica, Arial, sans-serif;
108+
}
109+
110+
.modal-header {
111+
margin-top: 0;
112+
font-weight: bold;
113+
font-size:0.95em;
114+
}
115+
116+
.modal-body {
117+
margin: 16px 0 50px 0;
118+
font-size:0.95em;
119+
}
120+
121+
.modal-default-button {
122+
width: 100%;
123+
height: 50px;
124+
text-align: center;
125+
background-color: #333333;
126+
border: none;
127+
color: white;
128+
font-size:0.95em;
129+
}
130+
131+
.modal-enter {
132+
opacity: 0;
133+
}
134+
135+
.modal-leave-active {
136+
opacity: 0;
137+
}
138+
139+
.modal-enter .modal-container,
140+
.modal-leave-active .modal-container {
141+
-webkit-transform: scale(1.1);
142+
transform: scale(1.1);
143+
}
144+
</style>
145+
146+
147+
<script>
148+
export default {
149+
methods: {
150+
show: function(title, message) {
151+
this.isShowModal = true;
152+
this.title = title;
153+
this.message = message;
154+
},
155+
close: function() {
156+
this.isShowModal = false;
157+
}
158+
},
159+
data() {
160+
return{
161+
isShowModal: false,
162+
title: '알림',
163+
message: ''
164+
}
165+
}
166+
}
167+
</script>
168+
```
169+
170+
### 플러그인 js 파일
171+
```js
172+
// alert-modal.js
173+
import AlertModalTemplate from './alert-modal-template.vue';
174+
175+
module.exports = {
176+
install(Vue) {
177+
var ModalConstructor = Vue.extend(AlertModalTemplate);
178+
var modalInstance = null;
179+
180+
window.AlertModal = function () {
181+
};
182+
183+
window.AlertModal.show = function (title, message) {
184+
if (modalInstance) {
185+
modalInstance.isShowModal = true;
186+
modalInstance.title = title;
187+
modalInstance.message = message;
188+
return;
189+
}
190+
191+
modalInstance = new ModalConstructor({
192+
el: document.createElement('div'),
193+
data() {
194+
return {
195+
title: title,
196+
message: message
197+
};
198+
}
199+
});
200+
modalInstance.isShowModal = true;
201+
document.body.appendChild(modalInstance.$el);
202+
};
203+
204+
window.AlertModal.close = function () {
205+
if (modalInstance) {
206+
modalInstance.isShowModal = false;
207+
return;
208+
}
209+
}
210+
}
211+
};
212+
```
213+
214+
### 사용자 코드
215+
```js
216+
import AlertModal from 'alert-modal'
217+
218+
Vue.use(AlertModal)
219+
AlertModal.show('알림', 'Hello, Vue.js!')
220+
221+
```
222+
223+
## 더 나은 시작을 위한 도구
224+
저는 간단한 플러그인을 제작했고 사내에서만 사용하기 때문에 플러그인 제작을 간단하게 했지만 만약 테스트, Lint와 같은 도구를 추가한다면 플러그인 작성이 힘들어질 수 있습니다. 그래서 이미 플러그인 제작을 위한 [vue-plugin-boilerplate](https://github.com/kazupon/vue-plugin-boilerplate)라는 `vue-cli` 템플릿이 등장했습니다. `vue-plugin-boilerplate`는 플러그인 제작을 위한 README와 같은 문서 작성, e2e 테스트, 유닛 테스트, Lint 등 다양한 작업을 미리 셋팅해주는 오픈소스입니다. 다음은 `vue-plugin-boilerplate`가 사용하는 툴입니다.
225+
226+
* Transpiler / Compiler
227+
* babel (for development)
228+
* buble (for distribution)
229+
* Type Checker
230+
* flowtype
231+
* Linter
232+
* eslint
233+
* Bundler
234+
* webpack (for development)
235+
* rollup (for distribution)
236+
* Test Assertion
237+
* power-assert
238+
* Test Framework
239+
* jasmine
240+
* Test Runner
241+
* karma
242+
* Test Coverage
243+
* istanbul
244+
* Headless Browser
245+
* phantomjs
246+
* End-to-End Test Fremework
247+
* nightwatch.js
248+
249+
사용자는 여기서 사용할 것과 사용하지 않을 것을 init할때 설정할 수 있습니다.
250+
251+
## 마치며..
252+
Vue.js는 아직 성장중은 오픈소스입니다. 만약 플러그인을 제작했다면 Vue.js 커뮤니티의 발전을 위해서 공개해보는건 어떨까요? :)

0 commit comments

Comments
 (0)