Anonymous Anonymous - 3 months ago
2004 0
Javascript

Vue component + fetch

<template>
  <div class="user">
    <h1>Id: {{ userId }}</h1>
    <p> {{ userName }}</p>
    <a :href="`${webSite}`" >{{ webSite }}</a>
    
  </div>
</template>

<script>
export default {
  name: "User",
  props: {
    id: Number
  },
  data() {
    return {
      username:String,
      website:String
    };
  },
  methods: {
    setUser: function(id){
      
      fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
      .then((res) => res.json())
      .then((res) =>{
        const {name, website} = res
        this.username = name
        this.website = website
      });
    }
  },
  computed: {
    userName() {
      return this.username
    },
    webSite() {
      return this.website
    },
    userId(){
      return this.id
    }
  },
  created() {
    this.user = this.setUser(this.id);
  }
};
</script>

<style scoped>
.user {
  background:#eee;
  width: 200px;
  padding:1em 2.5em;
  margin: 0 auto;
  border: 1px solid #ddd;
}
p{
  color: green;
  font-size:2em;
}

a{
  color: navy;
  text-decoration: none;
  font-size:1.5em;
}
</style>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download