Cristiano Magro
2024-12-30 ac100de8d8e0fc85e34cde9aec6262259b12f96c
07 - development setup/vue-cli-01-a-new-vue-project/src/components/FriendContact.vue
@@ -1,6 +1,6 @@
<template>
    <li>
        <h2>{{ name }} {{ friendIsFavorite === "1" ? '(Favorite)' : '' }}</h2>
        <h2>{{ name }} {{ isFavorite ? '(Favorite)' : '' }}</h2>
        <button @click="toggleDetails">
            {{ detailsAreVisible ? 'Hide' : 'Show' }} Details
        </button>
@@ -12,6 +12,7 @@
            <li><strong>Phone:</strong> {{ phoneNumber }}</li>
            <li><strong>Email:</strong> {{ emailAddress }}</li>
        </ul>
        <button @click="$emit('delete', id)">Delete</button>
    </li>
</template>
@@ -24,6 +25,10 @@
    //     'isFavorite'
    // ],
    props: {
        id: {
            title: String,
            required: true
        },
        name: {
            title: String,
            required: true
@@ -37,25 +42,27 @@
            required: true
        },
        isFavorite: {
            title: String,
            title: Boolean,
            required: false,
            default: '0',
            validator: function (value) {
                return value === '1' || value === '0';
            }
            default: false,
            // validator: function (value) {
            //     return value === '1' || value === '0';
            // }
        },
    },
    emits:['toggle-favorite', 'delete'],
    // emits: {
    //     'toggle-favorite': function (id) {
    //         if (id) {
    //             return true;
    //         } else {
    //             return false;
    //         }
    //     }
    // },
    data() {
        return {
            detailsAreVisible: false,
            friend: {
                id: "manuel",
                name: "Manuel Lorenz",
                phone: "01234 5678 991",
                email: "manuel@localhost.com",
            },
            friendIsFavorite: this.isFavorite,
        }
    },
    methods: {
@@ -63,12 +70,11 @@
            this.detailsAreVisible = !this.detailsAreVisible;
        },
        toggleFavorite() {
            if (this.friendIsFavorite === '1') {
                this.friendIsFavorite = '0';
            } else {
                this.friendIsFavorite = '1';
            }
            this.$emit('toggle-favorite', this.id);
        },
        // deleteFriend(){
        //     this.$emit('delete', this.id);
        // }
    },
};
</script>