screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> </head> <body> <h4>Vue Js Get Random item from array</h4> <div id="app"> <p v-if='randomItem'>Random item: {{ randomItem }}</p> <button @click="getRandomItem">Get random item</button> </div> <script type="module"> const app = Vue.createApp({ data() { return { myArray: ['item 1', 'item 2', 'item 3', 'item 4', 'item 5'], randomItem: '', } }, methods: { getRandomItem() { const randomIndex = Math.floor(Math.random() * this.myArray.length); this.randomItem = this.myArray[randomIndex]; }, }, }); app.mount('#app'); </script> </body> </html>