NGrdanjski
NGrdanjski

Reputation: 161

Vue generate router link

I have vue application for displaying videos. I want to automatic random generate router link every time when i click on

<router-link to="/video/this_value_to_be_random">Random video</router-link>

In component

<vue-video-background videoSrcMp4="path/to/your/video/file-this_value_to_be_random.mp4"></vue-video-background>

I want to pass this random value/number to this_value_to_be_random Purpose for this is every time when i click to link i want to display different video in the same component. Or different video path. Random number between 1-5.

Upvotes: 1

Views: 819

Answers (2)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You could add a method that generates a random link and bind it to to prop as follows :

  <router-link :to="randomLink()">Random video</router-link>
  ...

 methods:{
     randomLink(){
          let rnd=Math.floor(Math.random() * 5) + 1;
          return 'path/to/your/video/file_v'+rnd+'.mp4'
          }
      }

Upvotes: 1

Faure Ugo
Faure Ugo

Reputation: 69

In Js you can use :

Math.floor(Math.random() * 10) + 1 

This will give you a number between 1 and 10

Upvotes: 0

Related Questions