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

请教下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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件