2017-06-27 28 views
1

我试图显示按钮点击推送通知。Angular 2:无法读取未定义的属性'然后'推送通知

林坚持它说一个角承诺错误:

Cannot read property 'then' of undefined

完整的错误堆栈跟踪:

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)

我使用的是GitHub的链接:

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

我的 app.module.ts是:

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> 

如果删除了线:(load)="notification.show()看起来不错,但它没有任何通知没有通知点击按钮后!

+0

为什么你AppComponent布置得如同一个组件,然后像指令 –

+0

在组件中做什么指令? –

+0

请问能否纠正我的文件! – phyme

回答

0

错误是在这里:

app.component.ts中删除此代码块

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

注:

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

添加模块部分,你的意思是添加@Directive({在app.module.ts文件中? – phyme

+0

PushNotificationComponent已经存在,所以不用担心,然后再试一次 –

+0

你可以详细说明哪些文件需要修改,我是否应该创建一个名为:app.directive.ts的文件并在该文件中添加@Directive({push-notification' })? – phyme

相关问题