Li1438303345 2022-01-22 23:28 采纳率: 100%
浏览 120
已结题

请教下Vue中【随机数+url】达到随机图片效果的问题

做个留言板想实现随机头像,方法写出来了,没系统学过vue,不知道怎么调用,请教一下各位!

data() {
    return {
      chat: {
      **  img:  ··········  ),  ** //想实现随机图片的路径
        name: "",
        content: "",
        time: new Date().toLocaleDateString(),
      },
//计算属性
  computed: {
    num: function () {
      let num = Math.floor(Math.random() * 10 + 1);
      return (num = "../Picture/img/" + num + ".jpg");
    },
  },
//方法
  methods: {
    random_img() {
      let num = Math.floor(Math.random() * 10 + 1);
      return (num = "../Picture/img/" + num + ".jpg");
    },

  • 写回答

1条回答 默认 最新

  • 前端互助会 2022-01-23 00:28
    关注
    
    methods:{
      getImg(){
          var tips = [
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/18714484ed0bcedd.jpeg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/6c1de67f16cd473a.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/f1e72ea4509bfdc3.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/0e08adcc12a84823.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/c10aede244ea0307.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/6aa6957f01aaf8cf.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/ffafc9b499c87516.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/37f229f7abf99d89.jpg"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/8ffaa2489786e6f8.webp"},
                        {href:"https://ask.csdn.net/questions/7633263?answer=53670841",imgUrl:"https://ftp.bmp.ovh/imgs/2021/06/a4d652910bbb7826.jpg"},
                    ]
     
    var newArr = this.getRandomArrayElements(tips, 5)
     
    for (var i=0;i<newArr.length;i++){
        document.write("<a href='"+newArr[i].href+"'  target='_Blank'><img src='"+newArr[i].imgUrl+"' style='width:150px;height:150px;'></a>");
    
      },
    getRandomArrayElements(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月31日
  • 已采纳回答 1月23日
  • 创建了问题 1月22日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算