我想在角度对象中添加一个新方法,但是我使用@ types / angular,如何扩展angular.d.ts?

时间:2018-10-22 09:18:17

标签: typescript

例如,我想在angular中添加一个registerController方法,我将得到 Property 'registerController ' does not exist on type 'IAngularStatic'

我通过创建一个* .d.ts来解决这个问题

import * as angular from 'angular';

declare module 'angular' {
  interface IAngularStatic {
    registerController : any;
  }
}

但是为什么我应该使用import * as angular from 'angular'///<reference types="angular">不起作用

1 个答案:

答案 0 :(得分:0)

只需编写您自己的接口并像这样扩展MessageLabel = new Label { FontAttributes = FontAttributes.None, FontSize = 18, HorizontalTextAlignment = TextAlignment.Start, VerticalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.StartAndExpand, TextColor = Color.Black }; ScrollView scroll = new ScrollView() { Orientation = ScrollOrientation.Vertical }; scroll.Content = MessageLabel;

IAngularStatic

,而改用import {IAngularStatic} from "angular"; export interface ExtendedAngularStatic extends IAngularStatic { registerController: any; }