[Vuejs]-Vuetify – How to change v-img source with setTimeout function

1πŸ‘

βœ…

It’s not recommended to manipulate DOM as you’re doing, try to create a data property called url and change it where you want, for example set the setTimeout in the created hook as follows:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      url: 'https://www.w3schools.com/js/landscape.jpg',
    }
  },
  created(){
   setTimeout(()=>{
   this.url="https://www.w3schools.com/js/smiley.gif"},5000)
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

<div id="app">
  <v-app>
    <v-content>
      <v-container>
      <v-card class="d-flex flex-column"> <v-img id="image" :src="url" height="600"> <v-card-title class="headline"> Search for <v-spacer></v-spacer> <span class="caption font-italic font-weight-light">yes</span> </v-card-title> </v-img> </v-card>
      </v-container>
    </v-content>
  </v-app>
</div>

Leave a comment