[英]How to implement a strategy pattern for Vue components
事情就是這樣。 我有一個Cat
、 Dog
和Horse
,它們都實現了Animal
接口。 對於其中的每一個,我都有一個DogComponent
、 CatComponent
和HorseComponent
。
問題:
如何創建一個AnimalComponent
組件,它接受任何Animal
但在它是Dog
的情況下呈現DogComponent
而不使用switch-case
或<template>
中的一堆v-if
?
請假設僅渲染不同的imageUrl
和title
是不夠的。
只需使用Vue 動態組件:)
這個特殊的組件根據is
屬性中提供的名稱呈現一個組件。
<template>
<component v-for="animal in animals" :is="`${animal.type}-component`"/>
</template>
<script>
// vue syntax omitted for simplicity
animals: [
{ type: 'dog' },
{ type: 'cat' },
]
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.