λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ“  archive

[Vue.js] μ˜ˆμ‹œμ™€ ν•¨κ»˜ λ³΄λŠ” 동적 μ»΄ν¬λ„ŒνŠΈ

by HandHand 2022. 3. 1.

πŸ“Œ Vue 동적 μ»΄ν¬λ„ŒνŠΈ

μ΄λ²ˆμ— μ‚¬λ‚΄μ—μ„œ κ°œλ°œν•˜λŠ” μ•±μ˜ ν™”λ©΄ νŽΈμ§‘ κΈ°λŠ₯을 μΆ”κ°€ν•˜λ©΄μ„œ

동적 μ»΄ν¬λ„ŒνŠΈ 에 λŒ€ν•΄μ„œ μ°Ύμ•„λ³΄κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

νŽΈμ§‘ κΈ°λŠ₯을 톡해 μƒν’ˆ μΉ΄λ“œμ˜ μˆœμ„œλ₯Ό λ³€κ²½ν•΄μ•Ό ν•˜λŠ”λ°, ν™”λ©΄ λ¦¬ν”„λ ˆμ‹œκ°€ λ°œμƒν•˜μ§€ μ•Šκ³ 

DOM 의 μˆœμ„œλ₯Ό μ‘°μž‘ν•΄μ•Όν•˜κΈ° λ•Œλ¬Έμ— 이λ₯Ό μœ„ν•œ 방법이 λ°”λ‘œ 동적 μ»΄ν¬λ„ŒνŠΈ μ˜€μŠ΅λ‹ˆλ‹€.

 

πŸ“Œ 동적 μ»΄ν¬λ„ŒνŠΈλž€?

동적 μ»΄ν¬λ„ŒνŠΈ λŠ” 말 κ·ΈλŒ€λ‘œ λ Œλ”λ§λ˜λŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λ™μ μœΌλ‘œ λ³€κ²½ κ°€λŠ₯ν•œ 것을 μ˜λ―Έν•©λ‹ˆλ‹€.

λ”°λΌμ„œ νŠΉμ • 쑰건에 따라 μ„œλ‘œ λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈλ₯Ό 마운트 ν•˜λŠ” 것이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

 

<template>
  <div>
    <component :is="currentComponent" />
    <button @click="onClick" >클릭</button>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'

import Car from '../components/Car.vue'
import Finance from '../components/Finance.vue'

@Component({
  name: 'Index',
  components: { Car, Finance }
})
export default class Index extends Vue {
  private isCar = false

  public get currentComponent() {
    return this.isCar ? Car : Finance
  }

  public onClick() {
    this.isCar = !this.isCar
  }
}
</script>

<style>

</style>

 

λ™μž‘ μ˜ˆμ‹œλ₯Ό μœ„ν•œ κ°„λ‹¨ν•œ μ˜ˆμ œμž…λ‹ˆλ‹€.

component 의 is 속성에 λ Œλ”λ§ν•˜κ³ μž ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό μ§€μ •ν•΄μ„œ

λ™μ μœΌλ‘œ μ»΄ν¬λ„ŒνŠΈλ₯Ό ν• λ‹Ήν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ°˜μ‘ν˜•

πŸ’¬ λŒ“κΈ€