[Vuejs]-How make random tip picker in Nuxt?

2👍

Instead of replacing the value of randomcity with your Math.random() statement, consider using a computed getter method to create the randomness:

Remove this:

mounted: function(){
  this.randomcity = Math.random() * 1000
 },

Rename this:

randomcity: ["Rome", "Amsterdam", "Paris", "Berlin", "London", "Athens", "Madrid"],
// -> 
cities: ["Rome", "Amsterdam", "Paris", "Berlin", "London", "Athens", "Madrid"],

Now create your computed property:

computed: {
  randomcity () {
    return this.cities[Math.floor(Math.random()*this.cities.length)]
  }
}

Now you will get a random city from your array list.

Leave a comment