vuejs로 사이드 프로젝트를 진행하면서, chart를 추가하게 되었다.
mixins을 사용하여 chart를 여러 곳에서 사용할 수 있도록 하기로 했다.
그러던 중 extends를 알게 되었고, 둘의 차이를 정리하기로 했다.
// App.vue
<template>
<div id="app" class="color-red">
{{ num }} <br>
{{ msg }} <br>
<button @click="clicked">click</button>
</div>
</template>
<script>
import Extends from './components/Extends.vue'
import Mixins1 from './components/Mixins1.vue'
import Mixins2 from './components/Mixins2.vue'
import Mixins3 from './components/Mixins3'
export default {
name: 'App',
extends: Extends,
mixins: [Mixins1, Mixins2, Mixins3],
created() {
console.log('App created');
},
methods: {
clicked() {
alert('App clicked!');
}
}
}
</script>
// Extends.vue
<template>
<div>Extends</div>
</template>
<script>
export default {
name: 'Extends',
data() {
return {
num: 1,
msg: 'Extends data'
}
},
created() {
console.log('Extends created')
},
methods: {
clicked() {
alert('Extends methods')
}
}
}
</script>
// Mixins1.vue
<script>
export default {
name: 'Mixins1',
data() {
return {
msg: 'Mixins1 data'
}
},
created() {
console.log('Mixins1 created')
},
methods: {
clicked() {
alert('Mixins1 methods')
}
}
}
</script>
// Mixins2.vue
<script>
export default {
name: 'Mixins2',
data() {
return {
msg: 'Mixins2 data'
}
},
created() {
console.log('Mixins2 created')
},
methods: {
clicked() {
alert('Mixins2 methods')
}
}
}
</script>
<style scoped>
.color-red {
color: red;
}
</style>
// Mixins3.js
export default {
name: 'Mixins3',
created() {
console.log('Mixins3 created');
},
methods: {
clicked() {
alert('Mixins3 clicked!');
}
}
}
참고
https://vuejs.org/v2/guide/mixins.html
https://dev.to/fdietz/vue-js-mixins-extending-components-and-high-order-components-2gfb
https://blog.jungbin.kim/web/2019/01/21/js-vue-mixin-extend.html