나는 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
입니다. 예를 들어 프로젝트에서 나중에 컴포넌트가 필요한 뷰가 더 많이 있다면? 좋은 깨끗한 읽을 수있는 재사용 가능한 구성 요소를 원한다면 구성 요소의 경로를 계속 확인할 수 없습니다.
동일한보기가 필요한 경로를 처리하는 기본 방법은 무엇입니까?