1

Ich versuche, eine Push-Benachrichtigung beim Klicken auf die Schaltfläche anzuzeigen.Angular 2: Kann die Eigenschaft 'then' von undefined für die Push-Benachrichtigung nicht lesen

Im mit einem Winkel Versprechen Fehler stecken, die sagt:

Cannot read property 'then' of undefined

Voll Fehler-Stack-Trace:

Uncaught Error: Error in ./AppComponent class AppComponent - inline template:17:2 caused by: Cannot read property 'then' of undefined

TypeError: Cannot read property 'then' of undefined at PushNotificationComponent.show (notification.component.js:42) at DebugAppView.View_AppComponent0.handleEvent_16 (/AppModule/AppComponent/component.ngfactory.js:129) at DebugAppView.eventHandler (view.js:664) at HTMLButtonElement.COMPONENT_REGEX (dom_renderer.js:489) at ZoneDelegate.invokeTask (zone.js:367) at Object.NgZone.forkInnerZoneWithAngularBehavior.inner.inner.fork.onInvokeTask (ng_zone.js:264) at ZoneDelegate.invokeTask (zone.js:366) at Zone.runTask (zone.js:166) at HTMLButtonElement.cancelFn.invoke (zone.js:420)

Ich bin mit dem die Github Link:

https://github.com/alexcastillo/ng2-notifications/blob/master/src/app/app.component.html

Mein app.module.ts ist als:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 

import { PushNotificationComponent } from 'ng2-notifications/src/app/components/notification.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    PushNotificationComponent 
    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule 

    ], 
    providers: [], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

app.component.ts

import { Component } from '@angular/core'; 
import { 
    Directive, 
    EventEmitter, 
    OnInit, 
    OnChanges, 
    OnDestroy, 
    Input, 
    Output 
} from '@angular/core'; 

import '../style/app.scss'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.scss'] 
}) 
@Directive({ 
selector: 'push-notification' 
}) 
export class AppComponent { 
    title: string = 'ng2-notifications'; 
    description: string = 'Angular 2 Component for Native Push Notifications'; 

    constructor() { 
    } 
} 

app.component.html

<header> 
    <h1 class="title">{{ title }}</h1> 
</header> 
<main> 

    <h2>{{ description }}</h2> 

    <img src="img/demo.gif"> 

    <push-notification #notification title="ng2-notifications" body="Native Push Notifications in Angular 2" icon="<some_ulr>" closeDelay="2000" (load)="notification.show()"> //Problem Here!! 
    </push-notification> 

    <button (click)="notification.show()">Show Notification</button> 

</main> 

Wenn i die Zeile entfernen : (load)="notification.show() es scheint in Ordnung, aber es dient keinem Zweck ohne eine Notificati auf nach dem Klick klicken!

+0

warum Ihre AppComponent wie eine Komponente eingerichtet und dann wie eine Direktive –

+0

was macht eine Direktive in einer Komponente? –

+0

Könnten Sie meine Datei korrigieren? – phyme

Antwort

0

Fehler sind hier:

diesen Codeblock aus app.component.ts entfernen

@Directive({ 
selector: 'push-notification' 
}) 

Hinweis:

https://github.com/alexcastillo/ng2-notifications is coded with beta version of angular, so find the newer version or different plugin.

Given Doc is also regarding to the beta version of angular

+0

hinzufügen in Modulteil, Sie @Directive hinzufügen ({in app.module.ts Datei? – phyme

+0

PushNotificationComponent ist bereits da, also keine Sorge, und versuchen Sie es erneut –

+0

Könnte Sie bitte eloborate auf welche Datei muss ich ändern, Sollte ich eine Datei namens app.directive.ts erstellen und die @Directive ({ Selektor: 'Push-Notification' }) in dieser Datei hinzufügen? – phyme

Verwandte Themen