属性Razorpay在类型窗口上不存在

时间:2019-03-01 06:42:54

标签: angular

在倒数第二行,我得到错误Property Razorpay does not exist on type window。 该应用程序可以正常运行,并且不会出现任何错误,并且支付网关可以正常工作。由于存在错误,vsCode将文件突出显示为红色,但该文件成功编译,然后在命令行界面下面显示错误消息。如何摆脱这个错误。

export class InvoiceDetailComponent implements OnInit {
      _id: Number;
      singleInvoiceDetail: any;

      rzp1: any;
      title = 'app';
      options = {
        'key': 'dsewweewew',
        'amount': '2000', 
        'name': 'Merchant Name',
        'description': 'Purchase Description',
        'image': 'http://localhost:4200/theme/images/logo.svg',
        'handler': function(response: any) {
            alert(response.razorpay_payment_id);
        },
        'prefill': {
            'name': 'dsdsdsds',
            'email': 'dshil@razorpay.com'
        },
        'notes': {
            'address': 'Hello World'
        },
        'theme': {
            'color': '#F37254'
        }
      };

      constructor(
        private activatedRoute: ActivatedRoute, 
        private auth: AuthService, private router: Router, 
        @Inject(WINDOW)private window: Window) { }

      ngOnInit() {

      }

      public initPay(): void {
        console.log(window);
        this.rzp1 = window.Razorpay(this.options);
        this.rzp1.open();
      }
}

正在导入的窗口服务文件

import { isPlatformBrowser } from "@angular/common";
import { Injectable } from '@angular/core';
import { ClassProvider, FactoryProvider, InjectionToken, PLATFORM_ID } from '@angular/core';

/* Create a new injection token for injecting the window into a component. */
export const WINDOW = new InjectionToken('WindowToken');

/* Define abstract class for obtaining reference to the global window object. */
export abstract class WindowRef {

  get nativeWindow(): Window | Object {
    throw new Error('Not implemented.');
  }

}

/* Define class that implements the abstract class and returns the native window object. */
export class BrowserWindowRef extends WindowRef {

  constructor() {
    super();
  }

  get nativeWindow(): Window | Object {
    return window;
  }

}

/* Create an factory function that returns the native window object. */
export function windowFactory(browserWindowRef: BrowserWindowRef, platformId: Object): Window | Object {
  if (isPlatformBrowser(platformId)) {
    return browserWindowRef.nativeWindow;
  }
  return new Object();
}

/* Create a injectable provider for the WindowRef token that uses the BrowserWindowRef class. */
const browserWindowProvider: ClassProvider = {
  provide: WindowRef,
  useClass: BrowserWindowRef
};

/* Create an injectable provider that uses the windowFactory function for returning the native window object. */
const windowProvider: FactoryProvider = {
  provide: WINDOW,
  useFactory: windowFactory,
  deps: [ WindowRef, PLATFORM_ID ]
};

/* Create an array of providers. */
export const WINDOW_PROVIDERS = [
  browserWindowProvider,
  windowProvider
];

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。它将window对象强制转换为any,以便编译器不会抱怨。问题是脚本添加了Razorpay,因此编译器对此一无所知。

工作代码:

(this.window as any).Razorpay(this.options)