• uni-app : 生成三位随机数、自定义全局变量、自定义全局函数、传参、多参数返回值


    核心代码 

    1. function generateRandomNumber() {
    2. const min = 100;
    3. const max = 999;
    4. // 生成 min 到 max 之间的随机整数
    5. // Math.random() 函数返回一个大于等于 0 且小于 1 的随机浮点数。通过将其乘以 (max - min + 1),我们得到一个大于等于 0 且小于等于 (max - min + 1) 的随机浮点数。
    6. // 接着,我们将结果舍去小数部分,即使用 Math.floor() 方法对结果进行向下取整。
    7. // 最后,我们加上 min,这样就将取整后的结果偏移为在 min 到 max 之间的随机整数。
    8. const randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
    9. return randomNumber;
    10. }

    这里我采用全局函数进行书写

    自定义全局方法介绍

    目录

    效果

    全局文件代码(common.js)

    1. //定义全局变量
    2. const info = '我是全局变量'
    3. //定义全局函数
    4. //生成随机三位数
    5. function generateRandomNumber() {
    6. const min = 100;
    7. const max = 999;
    8. const randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
    9. return randomNumber;
    10. }
    11. //导出
    12. module.exports = {
    13. generateRandomNumber,
    14. info
    15. }

    主页面代码(index.vue)

    1. <template>
    2. <view>
    3. <view>{{info}}view>
    4. <view>{{random}}view>
    5. view>
    6. template>
    7. <script>
    8. import common from "../../utils/common.js"
    9. export default {
    10. data() {
    11. return {
    12. info:common.info,
    13. random:common.generateRandomNumber()
    14. }
    15. },
    16. methods: {
    17. }
    18. };
    19. script>
    20. <style>
    21. style>

    1、引入文件

            import common from "../../utils/common.js"

    2、调用自定义全局变量

            common.info

    3、调用自定义全局函数

            common.generateRandomNumber() 

    扩展 - 参数传递,返回值为多个数据

    效果

    主页面同上

    打印结果

    全局文件代码(common.js)

    1. //定义全局变量
    2. const info = '我是全局变量'
    3. //定义全局函数
    4. //生成随机三位数
    5. function generateRandomNumber() {
    6. const min = 100;
    7. const max = 999;
    8. const randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
    9. return randomNumber;
    10. }
    11. //参数传递
    12. function parameters(a,b,c){
    13. return {
    14. a,b,c
    15. };
    16. }
    17. //导出
    18. module.exports = {
    19. generateRandomNumber,
    20. info,
    21. parameters
    22. }

    return:返回多个参数

    扩展

    1、返回一个对象:

    1. function myFunction() {
    2. const result1 = 'hello';
    3. const result2 = 'world';
    4. const result3 = 123;
    5. return {
    6. value1: result1,
    7. value2: result2,
    8. value3: result3
    9. };
    10. }
    11. // 调用函数并获取返回值
    12. const { value1, value2, value3 } = myFunction();
    13. console.log(value1, value2, value3);

    2、返回一个数组

    1. function myFunction() {
    2. const result1 = 'hello';
    3. const result2 = 'world';
    4. const result3 = 123;
    5. return [result1, result2, result3];
    6. }
    7. // 调用函数并获取返回值
    8. const [value1, value2, value3] = myFunction();
    9. console.log(value1, value2, value3);

    主页面代码(index.vue)

    1. <template>
    2. <view>
    3. <view>全局变量:{{info}}view>
    4. <view>随机三位数:{{random}}view>
    5. view>
    6. template>
    7. <script>
    8. import common from "../../utils/common.js"
    9. export default {
    10. data() {
    11. return {
    12. info:common.info,
    13. random:common.generateRandomNumber()
    14. }
    15. },
    16. methods: {
    17. },
    18. onLoad() {
    19. //调用全局函数传参
    20. //这里传递的实参,分别对应形参中的a,b,c
    21. var parameters1 = 1;
    22. var parameters2 = 2;
    23. var parameters3 = 3;
    24. console.log(common.parameters(parameters1,parameters2,parameters3))
    25. }
    26. };
    27. script>
    28. <style>
    29. style>

    引用方法时传递参数:common.parameters(parameters1,parameters2,parameters3)

     

  • 相关阅读:
    必看!2023年最新MSP开源应用程序指南电子书大揭秘
    八、Nacos配置管理(统一配置管理、配置热更新、配置共享)
    Security ❀ 安全设备学习规范(第二版)
    【Head First 设计模式】-- 策略模式
    基于SpringBoot的健身房管理系统
    Linux ps -ef|grep去除 grep --color=auto信息
    MySQL:查询时进行时间比较
    phpstudy安装imagick扩展
    HTML5基础:框架,文字,图片,表格,列表
    矢量绘图软件源码定制开发,类似visio绘图,大量复合图元模板,可编程动态控制图元
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/133784342