salesforce - 如何在 Salesforce 中读取图像 URL 并使用 Salesforce 显示图像

标签 salesforce

我想构建一个 salesforce 组件,用户将在其中提供图像链接并且该组件将显示图像。所以我尝试了以下代码 闪电组件

<aura:component implements="flexipage:availableForAllPageTypes" access="global" 
 controller="MyController" >
 <lightning:card title="Partner Information">
 <Div>
     <p><lightning:input aura:name="image" label ="Enter image url"  type="text"/></p>

     <br></br> 
     <img url="{!image}" />
    </Div>
    </lightning:card>

   </aura:component>

但是插入图片url后不显示图片 我也尝试过使用该选项

     <img url="{!v.image}" /> 

但我收到错误消息访问检查失败! AttributeSet.get():组件的属性'image' 你能指导我显示图像吗?

最佳答案

因此,处理此问题的正确方法是使用 aura:html 组件。您需要为此设置一些参数:

  1. aura:id- String- 用​​于在 JS 中定位元素的 id
  2. tag- String- html 标签
  3. HTMLAttributes- Map- 无需在 .cmp 上显式设置,我们将使用 Js
<!--YourComponent.cmp-->
<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable" access="global" >
    <!--Remember to define your binding attribute-->
    <aura:attribute name="image" type="String"/>
    <lightning:card title="Partner Information">
        <div class="slds-p-around_medium">
            <p>
                <!--Set you attribute as the value of the lightning:input component-->
                <lightning:input aura:name="image" 
                                 label ="Enter image url" 
                                 value="{!v.image}" 
                                 type="text" 
                                 onchange="{!c.handleUrlChange}"/>
            </p>
            <br/>
            <!--Set the aura:id so you can access it in js, use component.find to locate-->
            <aura:html aura:id="imgDiv" tag="img"/>
        </div>
    </lightning:card>
</aura:component>
//YourComponentController.js
({
    handleUrlChange : function(component, event, helper) {
        //Grab the value from the attribute or you can use event.getParam("value");
        var imageUrl = component.get("v.image");
        //Define the map, find the img tag generated by aura:html, 
        //and set the HTMLAttributes to your map
        var newMapAttributes = {"src": imageUrl};
        component.find("imgDiv").set("v.HTMLAttributes",newMapAttributes);
    }
})

您还在图像标记上使用 url 属性,而不是 src 属性。您也许可以直接绑定(bind)到 img 元素,但 aura:html 效果非常好。希望这能回答您的问题!

关于salesforce - 如何在 Salesforce 中读取图像 URL 并使用 Salesforce 显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60091958/

相关文章:

salesforce - SOQL 查询 - 如何通过将字段设为小写并进行比较来编写 SOQL 查询?

rest - Salesforce API - 使用复合字段。 (无法从 VALUE_STRING 反序列化 MailingAddress 的实例)

properties - 覆盖 Salesforce Apex 中抽象类的属性

salesforce - 尝试在 SOQL 查询中避免 Governor 限制

database - Mule:将 470.000 条记录插入 Salesforce,每次迭代只允许 200 条记录

salesforce - SalesForce 中的 soql 连接查询

c# - 使用元数据 API 和 C# 从 Salesforce 检索国家/州和行业列表

java - 外部设置日志级别log4j

salesforce - 如何在 apex salesforce 中创建每分钟运行的计划作业

java - 如何在 Java 代码中正确转义 SOQL 查询中的特殊字符(如 - ')?