我有以下闪电网络组件来读取 JSON 字符串并将它们显示在联系人记录详细信息页面中。请注意,我是照明 Web 组件的新手,并且在学习方面付出了相当大的努力。
我的组件.html
<template>
<lightning-record-form
object-api-name={contactObject}
fields={myFields}
onsuccess={handleContactCreated} onload={handleContactInitialized} >
</lightning-record-form>
</template>
我的组件.js
import { LightningElement, wire, track } from 'lwc';
import findDetails from
'@salesforce/apex/JSONDemoController.getContactWithRelatedDataById';
import CONTACT_OBJECT from '@salesforce/schema/Contact';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TEST_FIELD from '@salesforce/schema/Contact.TestField__c';
import SPOUSE_FIELD from '@salesforce/apex/ResponseJSONWrapper.spouse';
import ADDRESS_FIELD from
'@salesforce/apex/ResponseJSONWrapper.mailingAddress';
export default class ContactCreator extends LightningElement {
contactObject = CONTACT_OBJECT;
myFields = [SPOUSE_FIELD,ADDRESS_FIELD];
@track contacts;
@track error;
handleContactCreated(){
// Run code when account is created.
}
handleContactInitialized(){
findDetails()
.then(result => {
var responseObj = JSON.parse(result.getReturnValue());
this.SPOUSE_FIELD = responseObj.spouse;
this.ADDRESS_FIELD = responseObj.mailingAddress;
})
.catch(error => {
this.error = error;
});
myFields = [SPOUSE_FIELD,ADDRESS_FIELD];
}
}
JSONDemoController.cls
public class JSONDemoController {
@AuraEnabled
public static String getContactWithRelatedDataById() {
String response = '';
ResponseJSONWrapper wrapper = new ResponseJSONWrapper();
wrapper.spouse = 'Test Spouse';
wrapper.mailingAddress = 'Test Address';
response = JSON.serialize(wrapper);
return response;
}
}
响应JSONWrapper.cls
public with sharing class ResponseJSONWrapper {
public String spouse;
public String contactRecordType;
public Date birthDate;
public String mobile;
public String mailingAddress;
public String otherAddress;
public String languages;
public String level;
public String Description;
}
但是当它被渲染时,我没有得到我在闪电组件中硬编码的值。什么都没有,它是空的。有人可以帮助指出我哪里出错了吗?