Typescript:在静态函数中使用私有对象

时间:2017-01-09 09:05:08

标签: angular typescript angular-material

如何以漂亮的代码方式将Angular Material与Typescript一起使用?

例如,我不想写在我使用SnackBar的每个地方:

constructor(public snackBar: MdSnackBar) {}


***
this.snackBar.open('text', {
  duration: 500
}); // and i need to add this yet 10 times: no to good
***

我想创建一个单独的类,只调用它(静态函数),如:

constructor(private snackBar: MdSnackBar) {
}

public static showSnackBar(text: string, config: string): void {
  this.snackBar.open(text, config);
}

但是我得到了错误:

Property 'snackBar' does not exist on type 'typeof SnackBar'.

当我转换代码时:

  static snackBar: MdSnackBar;

  constructor() {
  }

  public static showSnackBar(text: string, config: string): void {
    SnackBar.snackBar.open(text);
  }

从另一个分类中调用它,我得到了:

EXCEPTION: Cannot read property 'open' of undefined
ErrorHandler.handleError @ error_handler.js:47
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
ZoneDelegate.handleError @ zone.js:236
Zone.runTask @ zone.js:157
ZoneTask.invoke @ zone.js:335
error_handler.js:52 ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:52
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
ZoneDelegate.handleError @ zone.js:236
Zone.runTask @ zone.js:157
ZoneTask.invoke @ zone.js:335
error_handler.js:53 TypeError: Cannot read property 'open' of undefined
    at Function.SnackBar.showSnackBar (snack-bar.ts:13)
    at SafeSubscriber._next (edit.component.ts:78)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:223)
    at SafeSubscriber.next (Subscriber.js:172)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at CatchSubscriber.Subscriber._next (Subscriber.js:125)
    at CatchSubscriber.Subscriber.next (Subscriber.js:89)
    at MapSubscriber._next (map.js:83)
    at MapSubscriber.Subscriber.next (Subscriber.js:89)
ErrorHandler.handleError @ error_handler.js:53
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
ZoneDelegate.handleError @ zone.js:236
Zone.runTask @ zone.js:157
ZoneTask.invoke @ zone.js:335
Subscriber.js:227 Uncaught TypeError: Cannot read property 'open' of undefined
    at Function.SnackBar.showSnackBar (snack-bar.ts:13)
    at SafeSubscriber._next (edit.component.ts:78)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:223)
    at SafeSubscriber.next (Subscriber.js:172)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at CatchSubscriber.Subscriber._next (Subscriber.js:125)
    at CatchSubscriber.Subscriber.next (Subscriber.js:89)
    at MapSubscriber._next (map.js:83)
    at MapSubscriber.Subscriber.next (Subscriber.js:89)
SnackBar.showSnackBar @ snack-bar.ts:13
(anonymous) @ edit.component.ts:78
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
MapSubscriber._next @ map.js:83
Subscriber.next @ Subscriber.js:89
onLoad @ xhr_backend.js:72
ZoneDelegate.invokeTask @ zone.js:265
onInvokeTask @ ng_zone.js:227
ZoneDelegate.invokeTask @ zone.js:264
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:335

我做错了什么,我误解了什么?

2 个答案:

答案 0 :(得分:1)

如果是静态功能,您只能使用静态成员'这个'

答案 1 :(得分:0)

您可以在类中创建静态方法,然后按类名导入它们。

示例:

this.name = AStaticServiceClass.doStuff();

并在任何地方使用它:

<a-entity rotation="0 45 0">
  <a-box position="0 1 0"></a-box>
</a-entity>

导入课程。

Full plunker示例:http://plnkr.co/edit/NWRSuaUpzkPEP3gL1DKK?p=preview