假设我有给定的对象
export interface File {
id: string;
displayname: string;
filesize: number;
isarchived: boolean;
userId: string;
[key: string]: any;
}
和以下方法:
export class BasketService {
addFile(file: File ) { //Some code here }
}
比起我这样调用我的方法:
this._basketService.addFile({
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
???: file.originalEntry // <=== How can I set the key value pair here?
});
对象:
file.originalEntry is [key: string]
。我想传递整个对象
而不是单个键/值对。
答案 0 :(得分:1)
尝试这样:
this._basketService.addFile({
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
keyValue: {key:file.id, value: file.originalEntry}
});
答案 1 :(得分:1)
如果声明是这样的:
public dictionary: { [key: string]: string } = {};
您可以通过以下方式设置其值:
this.dictionary = { ID: key, Value: defaultValue }; // key and defaultValue are variables
根据您的情况,通过以下操作:
{ ID: key, Value: defaultValue }
答案 2 :(得分:0)
我解决了它,将我所需的键/值对明确地放在了这样的对象中:
const digitalObject = {
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
code: file.originalEntry['code'],
needle: file.originalEntry['needle'],
x8: file.originalEntry['x8'][0],
x5: file.originalEntry['x5'][0],
8: file.originalEntry['8'][0],
5: file.originalEntry['5'][0],
creationtime: file.creationTime
};
this._basketService.addFile(digitalObject);
感谢所有贡献。