728x90
Vue Cli를 통해 컴포넌트 만들어보기
- 아래와 같이 Vue CLI를 통해 AppHeader 컴포넌트를 만들고, Props 내리기, Event 올리기를 해보았다.
- data()로 달라진 점 주석에 설명이 되어 있으며, 해당 부분 이외엔 특별히 다른점이 없다.
- 이제 이정도면 Vue를 통해 개발을 시작할 준비가 되었다.
- 중급 강의를 들으러 가기 전 마지막 사용자 입력 폼 하나를 만들어 볼 예정이다.
//@ App.vue
<template>
<div>
<div>{{ str }}</div>
<!-- <AppHeader v-bind:프롭스 속성 이름="상위 컴포넌트의 데이터 이름"></AppHeader> -->
<AppHeader v-bind:propsdata="str" v-on:renew="renewStr"></AppHeader>
<!-- <app-header></app-header> -->
</div>
</template>
<script>
import AppHeader from './components/AppHeader.vue';
// let AppHeader = {
// template: '<header><h1>Header</h1></header>',
// };
// 인스턴스 옵션에서 data가 그냥 객체 리터럴이었는데,
// vue CLI에서는 data() 함수형태이다.
// 이는 컴포넌트를 재사용할 것이고,
// 데이터가 컴포넌트 간 간섭되지 않도록 하기 위함이다.
// new Vue({
// data: {
// str: 'hi',
// },
// });
export default {
data() {
return {
str: 'Header',
};
},
components: {
AppHeader,
},
methods: {
renewStr() {
this.str = 'hi';
},
},
};
</script>
<style scoped></style>
// @ components/AppHeader.vue
<template>
<header>
<h1>{{ propsdata }}</h1>
<button v-on:click="sendEvent">send</button>
</header>
</template>
<script>
export default {
props: ['propsdata'],
methods: {
sendEvent() {
this.$emit('renew');
},
},
};
</script>
<style scoped></style>
'FrontEnd > Vue 2' 카테고리의 다른 글
[Vue.js] 로컬스토리지 새로고침 문제 ;; (해결) (0) | 2022.09.28 |
---|---|
[Vue.js] style 태그에 있는 scoped의 의미 (0) | 2022.09.18 |
[Vue.js] Vue CLI를 활용해서 로그인 form 만들기 (0) | 2022.09.18 |
[Vue.js] vue cli와 이전 방식 비교 (0) | 2022.09.18 |
[Vue.js] Computed 속성을 활용한 CSS Class 바인딩 (0) | 2022.09.18 |
[Vue.js] Watch 속성 vs Computed 속성 (0) | 2022.09.18 |
[Vue.js] Watch 속성 (0) | 2022.09.18 |
[Vue.js] v-on 디렉티브를 통한 event Handle Methods binding (0) | 2022.09.18 |
댓글