<template> <div> {{ data }} </div> </template> <script> export default { name: 'index', data() { return { data:'' }; }, created() { this.getRandomInt(1,10) }, methods: { // 生成1-10的整數 // Math.floor(); 向下舍入 // console.log(Math.floor(3.8)); //向下舍入,小數點省略了 結果:3 // Math.random() random() 方法可返回介於 0(包含) ~ 1(不包含) 之間的一個隨機數。 // Math.floor((Math.random()*10)+1);取得介於 1 到 10 之間的一個隨機數: // Math.floor((Math.random()*100)+1);取得介於 1 到 100 之間的一個隨機數: getRandomInt(min, max) { // 以下函數返回 min(包含)~ max(包含)之間的數字: this.data = Math.floor(Math.random() * (max - min + 1)) + min // 函數返回 min(包含)~ max(不包含)之間的數字 // this.data = Math.floor(Math.random() * (max - min) ) + min; }, } }; </script>