emitting custom event
per passare dati da dentro il componente alla app soprastante
| | |
| | | <friend-contact |
| | | v-for="friend in friends" |
| | | :key="friend.id" |
| | | :id="friend.id" |
| | | :name="friend.name" |
| | | :phone-number="friend.phone" |
| | | :email-address="friend.email" |
| | | :is-favorite="true" |
| | | :is-favorite="friend.isFavorite" |
| | | @toggle-favorite="toggleFavoriteStatus" |
| | | ></friend-contact> |
| | | </ul> |
| | | </section> |
| | |
| | | name: "Manuel Lorenz", |
| | | phone: "01234 5678 991", |
| | | email: "manuel@localhost.com", |
| | | isFavorite: true, |
| | | }, |
| | | { |
| | | id: "julie", |
| | | name: "Julie Jones", |
| | | phone: "09876 543 221", |
| | | email: "julie@localhost.com", |
| | | isFavorite: true, |
| | | }, |
| | | ], |
| | | } |
| | | }, |
| | | methods:{ |
| | | toggleFavoriteStatus(friendId){ |
| | | const found = this.friends.find( |
| | | (friend) => friend.id === friendId |
| | | ); |
| | | found.isFavorite = !found.isFavorite; |
| | | } |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | |
| | | <template> |
| | | <li> |
| | | <h2>{{ name }} {{ friendIsFavorite ? '(Favorite)' : '' }}</h2> |
| | | <h2>{{ name }} {{ isFavorite ? '(Favorite)' : '' }}</h2> |
| | | <button @click="toggleDetails"> |
| | | {{ detailsAreVisible ? 'Hide' : 'Show' }} Details |
| | | </button> |
| | |
| | | // 'isFavorite' |
| | | // ], |
| | | props: { |
| | | id:{ |
| | | title: String, |
| | | required: true |
| | | }, |
| | | name: { |
| | | title: String, |
| | | required: true |
| | |
| | | data() { |
| | | return { |
| | | detailsAreVisible: false, |
| | | friendIsFavorite: this.isFavorite, |
| | | } |
| | | }, |
| | | methods: { |
| | |
| | | this.detailsAreVisible = !this.detailsAreVisible; |
| | | }, |
| | | toggleFavorite() { |
| | | this.friendIsFavorite = !this.friendIsFavorite; |
| | | // this.friendIsFavorite = !this.friendIsFavorite; |
| | | this.$emit('toggle-favorite', this.id); |
| | | }, |
| | | }, |
| | | }; |