私はa small app that lets you add, remove and edit recipes to a list of recipesです。VUE多目的コンポーネント?
追加にはルート/add
があり、編集にはルート/edit
があります。私はのためにAddRecipe
と呼ばれるコンポーネントを使用しています。どちらものルートです。
ルートに'edit'
が含まれていると、コンポーネントの動作がわずかに異なります。つまり、入力フィールドには編集中の値があらかじめ入力されています。私はこの問題へのよりよい解決策があります考えてい
<template>
<div>
<form class="form">
<input v-model="recipe.name" placeholder="Recipe Name">
<textarea v-model="recipe.description" placeholder="Recipe Description..." rows="10"></textarea>
<button :disabled="nothingEntered()" @click.prevent="addRecipe">Submit</button>
</form>
</div>
</template>
<script>
export default {
name: 'AddRecipeForm',
data() {
return {
recipe: this.isEdit()
? this.$store.state.recipes[this.$route.params.id]
: {
name: '',
description: ''
}
}
},
methods: {
isEdit() {
return this.$route.path.includes('edit')
},
addRecipe() {
if (this.isEdit()) {
this.$store.dispatch('addRecipe', this.recipe)
} else {
this.$store.dispatch('addRecipe', {
id: Date.now(),
...this.recipe
})
}
this.$router.push('/')
},
nothingEntered() {
return !this.recipe.name || !this.recipe.description
}
},
}
</script>
:
はここで、共有コンポーネントAddRecipeForm.vue
です。たとえば、コンポーネントを必要とするプロジェクトの後半に必要なビューがさらにある場合はどうなりますか?きれいで読みやすい再利用可能なコンポーネントが必要な場合、コンポーネントのルートをチェックし続けることができません。
同じビューを必要とするルートを扱う場合、どのような方法が適していますか?