在 salesforce lightning 中执行 showtoast 时出错
Error while executing showtoast in salesforce lightning
下面的代码在执行 toastEvent.setParams 语句时抛出错误。不确定我错过了什么或者它在 spring'19 中被弃用了吗?
loadContacts : function(cmp) {
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response){
var state = response.getState();
if (state === 'SUCCESS') {
cmp.set('v.contacts', response.getReturnValue());
cmp.set('v.contactList', response.getReturnValue());
this.updateTotal(cmp);
}
console.log('Here');
var toastEvent = $A.get("e.force:showToast");
if (state === 'SUCCESS') {
toastEvent.setParams({
"title" : 'Success!',
"message" : 'Your contacts have been loaded successfully.'
});
}
else {
toastEvent.setParams({
"title" : "Error!",
"message" : "Something has gone wrong."
});
}
toastEvent.fire();
});
$A.enqueueAction(action);
},
错误截图如下:
This event is handled by the one.app container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities.
当您尝试在 one.app 容器上下文之外抓取 $A.get("e.force:showToast")
时(例如,如果您将组件放在 Lightning 应用程序中进行测试而不是将其拖到生成器中的记录页面)。 $A.get("e.force:showToast")
返回为未定义,对未定义调用 setParams 会引发错误。
尝试将您的组件拖到记录详细信息页面或社区,或者从照明组件创建一个选项卡。如果您需要在 one.app 上下文之外使用该组件,您需要自己实现 show/hide toast 逻辑。
您可以使用 lightning:notificationsLibrary
。在你的 aura 组件中,添加:
<lightning:notificationsLibrary aura:id="notifLib"/>
并将您的辅助方法更改为:
loadContacts : function(cmp) {
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response){
var state = response.getState();
if (state === 'SUCCESS') {
cmp.set('v.contacts', response.getReturnValue());
cmp.set('v.contactList', response.getReturnValue());
this.updateTotal(cmp);
}
console.log('Here');
var toastEvent = $A.get("e.force:showToast");
if (state === 'SUCCESS') {
cmp.find('notifLib').showToast({
"title" : 'Success!',
"message" : 'Your contacts have been loaded successfully.'
"variant": 'success',
"mode": 'sticky'
});
}
else {
cmp.find('notifLib').showToast({
"title" : "Error!",
"message" : "Something has gone wrong."
"variant": 'error',
"mode": 'sticky'
});
}
});
$A.enqueueAction(action);
},
下面的代码在执行 toastEvent.setParams 语句时抛出错误。不确定我错过了什么或者它在 spring'19 中被弃用了吗?
loadContacts : function(cmp) {
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response){
var state = response.getState();
if (state === 'SUCCESS') {
cmp.set('v.contacts', response.getReturnValue());
cmp.set('v.contactList', response.getReturnValue());
this.updateTotal(cmp);
}
console.log('Here');
var toastEvent = $A.get("e.force:showToast");
if (state === 'SUCCESS') {
toastEvent.setParams({
"title" : 'Success!',
"message" : 'Your contacts have been loaded successfully.'
});
}
else {
toastEvent.setParams({
"title" : "Error!",
"message" : "Something has gone wrong."
});
}
toastEvent.fire();
});
$A.enqueueAction(action);
},
错误截图如下:
This event is handled by the one.app container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities.
当您尝试在 one.app 容器上下文之外抓取 $A.get("e.force:showToast")
时(例如,如果您将组件放在 Lightning 应用程序中进行测试而不是将其拖到生成器中的记录页面)。 $A.get("e.force:showToast")
返回为未定义,对未定义调用 setParams 会引发错误。
尝试将您的组件拖到记录详细信息页面或社区,或者从照明组件创建一个选项卡。如果您需要在 one.app 上下文之外使用该组件,您需要自己实现 show/hide toast 逻辑。
您可以使用 lightning:notificationsLibrary
。在你的 aura 组件中,添加:
<lightning:notificationsLibrary aura:id="notifLib"/>
并将您的辅助方法更改为:
loadContacts : function(cmp) {
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response){
var state = response.getState();
if (state === 'SUCCESS') {
cmp.set('v.contacts', response.getReturnValue());
cmp.set('v.contactList', response.getReturnValue());
this.updateTotal(cmp);
}
console.log('Here');
var toastEvent = $A.get("e.force:showToast");
if (state === 'SUCCESS') {
cmp.find('notifLib').showToast({
"title" : 'Success!',
"message" : 'Your contacts have been loaded successfully.'
"variant": 'success',
"mode": 'sticky'
});
}
else {
cmp.find('notifLib').showToast({
"title" : "Error!",
"message" : "Something has gone wrong."
"variant": 'error',
"mode": 'sticky'
});
}
});
$A.enqueueAction(action);
},