HTML 파일 안에서 url 경로를 포함시켜 CDN으로부터 Vue 라이브러리를 불러오는 방식이다.
최신버전 불러오기
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
특정버전 불러오기
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.0"></script>
<script>
new Vue({
el : '#app',
});
</script>
<script>
Vue.component('wordRelay', {
template: `
<div>
<p>{{currentWord}}</p>
<form v-on:submit="onSubmitForm">
<input type="text" v-model="answerText" v-ref="text">
<button type="submit">입력!</button>
</form>
<p style="color: skyblue">{{msg}}</p>
</div>
`,
props: ['startWord'],
data() {
return {
currentWord: this.startWord,
answerText:'',
msg:''
}
},
methods: {
onSubmitForm(e) {
e.preventDefault();
if(this.answerText.length > 0 && this.currentWord[this.currentWord.length - 1] === this.answerText[0]) {
this.msg = "굿굿";
this.currentWord = this.answerText;
}
else
this.msg = "땡~";
this.$refs.text.focus();
}
}
})
</script>
<body>
<div id="app">
<word-relay start-word="소나무"></word-relay>
<word-relay start-word="무지개"></word-relay>
<word-relay start-word="개나리"></word-relay>
</div>
</body>
npm으로 Vue 라이브러리를 불러오는 방식이다.
node.js 문법의 모듈들을 사용하기 위해서 Webpack이 필요하다.
$ npm init
$ npm install vue
$ npm install webpack webpack-cli --save-dev
const path = require('path');
module.exports = {
entry: {
app: path.join(__dirname, 'main.js'),
},
module: {},
plugins: [],
output: {
filename: 'app.js',
path: path.join(__dirname, 'dist'),
},
};
Webpack 설정파일인 webpack.config.js
는 node.js문법으로 쓰이며 기본적으로 위와 같은 구조를 가진다.
entry에 설정된 파일(./main.js
)은 root vue instance선언과 template 불러오기 등을 수행한다.
ouput에 설정된 파일(./dist/app.js
)은 webpack이 ./main.js
를 참고하여 번들링을 진행한 후 최종 결과물로 생성된 파일로, index.html
에서 불러와 사용한다.
import Vue form 'vue';
import WordReply from './WordReply.vue'
new Vue({ el : '#app' })
// TemplateName.vue
Vue.component('wordRelay', {
template: `
<div>
<p>{{currentWord}}</p>
<form v-on:submit="onSubmitForm">
<input type="text" v-model="answerText" v-ref="text">
<button type="submit">입력!</button>
</form>
<p style="color: skyblue">{{msg}}</p>
</div>
`,
props: ['startWord'],
data() {
return {
currentWord: this.startWord,
answerText:'',
msg:''
}
},
methods: {
onSubmitForm(e) {
e.preventDefault();
if(this.answerText.length > 0 && this.currentWord[this.currentWord.length - 1] === this.answerText[0]) {
this.msg = "굿굿";
this.currentWord = this.answerText;
}
else
this.msg = "땡~";
this.$refs.text.focus();
}
}
})
<body>
<div id="app"></div>
<word-relay start-word="소나무"></word-relay>
<word-relay start-word="무지개"></word-relay>
<word-relay start-word="개나리"></word-relay>
</body>
<script src="./dist/app.js"></script>