如何以一种漂亮的代码方式使用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
我做错了什么,我误解了什么?
3条答案
按热度按时间2ul0zpep1#
如果是静态函式,您只能使用'this'的static成员
wbrvyc0a2#
您可以在类别中建立静态方法,然后依类别名称汇入它们。
示例:
并将其用于任何需要的位置:
通过导入类。
完整柱塞示例:http://plnkr.co/edit/NWRSuaUpzkPEP3gL1DKK?p=preview
nbewdwxp3#
这是我在与问题内容相同的情况下使用的解决方案。
步骤1:创建一个类,其中包含使用snackbar显示消息的静态函数
第二步:更新app组件--将MatSnackBar设置到AlertMessageUtils类中。
第3步:现在我们可以在整个应用程序中使用AlertmessageUtils,只需调用它的静态函数,如下所示