我使用我的Angular应用程序使用js库进行一些测试,我注意到以下内容:
secret.service.ts:
@Injectable()
export class SecretService {
public get adalConfig(): any {
return {
clientId: 'e71122a4-3998-416d-xxxxx',
redirectUri: window.location.origin + '/',
callback: () => console.log('not called!!') // not work
};
}
app.component.ts
constructor(private adalService: AdalService,
private secretService: SecretService,) {
this.adalService.init(this.secretService.adalConfig);
}
当我在服务中注册callback
属性时,它永远不会调用。
但是,如果我将app.component.ts更改为:
constructor(private adalService: AdalService,
private secretService: SecretService,) {
let adalConfig: any = {
clientId: this.secretService.adalConfig.clientId,
redirectUri: window.location.origin + '/',
}
adalConfig.callback = () => console.log('now is called!') // like this works normally
this.adalService.init(adalConfig);
}
为什么我在服务上设置回调函数时没有调用?
答案 0 :(得分:0)
也许你省略了一些括号。试试这个:
this.adalService.init(this.secretService.adalConfig());
将adalConfig方法更改为:
adalConfig(): any {
return {
clientId: 'e71122a4-3998-416d-xxxxx',
redirectUri: window.location.origin + '/',
callback: () => console.log('not called!!') // not work
};
}
答案 1 :(得分:0)
adalConfig 是一种方法,您将像变量一样传递,以便访问您需要调用它的方法的返回值。
constructor(private adalService: AdalService, private secretService: SecretService,) {
console.log(typeof this.secretService.adalConfig);// you can see function
this.adalService.init(this.secretService.adalConfig());
}