【发布时间】:2020-08-04 16:38:00
【问题描述】:
我想通过传播一个params对象like this SO answer将所有参数传递给这个函数:
public showToast(
content: string,
buttonLabel = 'Ok',
hideDelay?: number,
buttonAction?: Function,
uniqueKey?: string,
canBeCleared?: boolean,
templateCtrl?: Object
): Toast {
const toast = new Toast(
content,
this,
buttonLabel,
hideDelay,
buttonAction,
uniqueKey,
canBeCleared,
templateCtrl
);
toast.id = !this.lastToast ? 1 : this.lastToast.id + 1;
this.toasts.push(toast);
toast.top = toast.fromTop;
// hiding is handled in the ToastComponent
return toast;
}
我的尝试:
const toastArgs = {
content: 'test toast',
buttonLabel: 'test button',
hideDelay: 1000,
buttonAction: () => 5,
uniqueKey: 'uk',
canBeCleared: true,
templateCtrl: { strl: 3 }
};
service.showToast(...Object.values(toastArgs));
编译时错误:
预期有 1-7 个参数,但得到了 0 个或更多.ts(2556) toast.service.ts(18, 9): 没有提供“内容”的参数。
为什么会出现此错误?我已经按照 SO 回答了。
【问题讨论】:
-
完整的错误消息、编译器选项和 TypeScript 的版本可能会有所帮助。
标签: javascript typescript ecmascript-6