zoukankan      html  css  js  c++  java
  • react img 被自动转成base64,无法根据当前路径来动态改变值的解决办法

    项目需求,需要根据当前的图片的路径值的来(加或者减)动态改变其值: state定义如下:

    this.state={
                basket01:0+require("../../img/egg/egg1_00.png"),
                basket02:0+require('../../img/egg/egg2_00.png'),
                basket03:0+require('../../img/egg/egg3_00.png'),
                basket04:0+require('../../img/egg/egg4_00.png'),
            }  

     重点就是前面的数值:

    render 代码如下:

    <img className="egg-pen" src={this.state.basket01.substring(1,this.state.basket01.length)} alt=""/>
    

      

    计算操作的方法代码为:

    add (index){
            let basket01Num = this.state.basket01.substring(0,1)
            let basket02Num = this.state.basket02.substring(0,1)
            let basket03Num = this.state.basket03.substring(0,1)
            let basket04Num = this.state.basket04.substring(0,1)
            let eggSum = this.state.eggSum
            this.setState({
                eggSum:--eggSum
            })
            if(index === 1){
                ++basket01Num
                this.setState({
                    basket01:basket01Num+require(`../../img/egg/egg1_0${basket01Num}.png`)
                })
            }else if(index === 2){
                ++basket02Num
                this.setState({
                    basket02:basket02Num+require(`../../img/egg/egg2_0${basket02Num}.png`)
                })
            }else if(index === 3){
                ++basket03Num
                this.setState({
                    basket03:basket03Num+require(`../../img/egg/egg3_0${basket03Num}.png`)
                })
            }else{
                ++basket04Num
                this.setState({
                    basket04:basket04Num+require(`../../img/egg/egg4_0${basket04Num}.png`)
                })
    
            }
        }
    

      

    项目实现的效果:

     通过加减来改变图片里面的鸡蛋数量

  • 相关阅读:
    设置共享文件夹大小
    About IConfigurationSectionHandler Interface
    zoj 1050
    SQL Server 数据库优化经验总结
    一、页面输出缓存
    [转]深入解读 ADO.NET 2.0 的十大最新特性
    ASP.NET 缓存学习
    [转]写给ASP.NET程序员:网站中的安全问题
    [转] ASP.NET 性能提升秘诀之管道与进程优化
    实战 SQL Server 2005 镜像配置
  • 原文地址:https://www.cnblogs.com/keleyz/p/9722014.html
Copyright © 2011-2022 走看看