Build a Vue.js Random User API Generator With Profile Photo & Email in Browser Using HTML5 & Javascript Full Project For Beginners

  • Post author:
  • Post category:Vue
  • Post comments:0 Comments

 

 

 

index.html

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@3.0.5"></script>
    <link rel="stylesheet" href="style.css">
    <title>Mini Project</title>
</head>
<body>
    <div id="app">
        <img v-bind:src="picture" v-bind:alt="`${firstname} ${lastname}`" v-bind:class="gender">
        <h1>{{ firstname }} {{ lastname }}</h1>
        <h3>Email: {{ email }}</h3>
        <button v-bind:class="gender" v-on:click="getUser()">Get Random User</button>
    </div>

    <script src = "app.js"></script>
</body>
</html>

 

 

style.css

 

 

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    font-family: Arial, Helvetica, sans-serif;
}

#app {
    width: 400px;
    height: 400px;
    margin: auto;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

h1, h3{
    margin-bottom: 1rem;
    font-weight: normal;
}

img{
    border-radius: 50%;
    border: 5px #333 solid;
    margin-bottom: 1rem;
}
.male {
    border-color : steelblue;
    background-color: steelblue;
}

.female {
    border-color: pink;
    background-color: pink;
    color : #333;
}

button {
    cursor: pointer;
    display: inline-block;
    background: #333;
    color: white;
    font-size: 18px;
    border: 0;
    padding: 1rem 1.5rem;
}

button:focus {
    outline: none;
}

 

 

app.js

 

 

const app = Vue.createApp({ 
    data (){
        return {
            firstname: 'John',
            lastname: 'Doe',
            email: 'jhon@gmail.com',
            gender: 'male',
            picture: 'https://randomuser.me/api/portraits/men/14.jpg',
        }
    },
    methods: {
        async getUser() {
            const res = await fetch('https://randomuser.me/api')
            const { results } = await res.json()

            // console.log(results);
            this.firstname = results[0].name.first
            this.lastname = results[0].name.last
            this.email = results[0].email
            this.gender = results[0].gender
            this.picture = results[0].picture.large

        }
    }
})

app.component("")

app.mount('#app')

Leave a Reply