2016-04-29 51 views
1

我有一個角度2應用程序,我今天從beta1升級到beta16。它的工作原理罰款β1,但不能在16我得到這個錯誤:EXCEPTION:TypeError:在升級後無法讀取undefined屬性'subscribe'

angular2.dev.js:24821 TypeError: Cannot read property 'subscribe' of undefined 
    at AppView._View_ApplicationForm0.createInternal (ApplicationForm.template.js:825) 
    at AppView.create (angular2.dev.js:22641) 
    at AppView._View_Home5.createInternal (Home.template.js:509) 
    at AppView.create (angular2.dev.js:22641) 
    at TemplateRef_.createEmbeddedView (angular2.dev.js:4186) 
    at ViewContainerRef_.createEmbeddedView (angular2.dev.js:5742) 
    at NgIf.Object.defineProperty.set [as ngIf] (angular2.dev.js:10003) 
    at AppView._View_Home0.detectChangesInternal (Home.template.js:848) 
    at AppView.detectChanges (angular2.dev.js:22811) 

我有是工作得很好plnkr一個例子,但是這並不在我的地方工作。只適用於beta1。這裏是我的plnkr:

http://plnkr.co/edit/eZJYOVsvFeglfJmuIHpp?p=preview

app.ts

import {Component} from 'angular2/core' 
import {Form} from 'src/frm' 
@Component({ 
    selector: 'my-app', 
    providers: [], 
    template: ` 
    <div> 
     <frm (submitted) = "submitted()"></frm> 
    </div> 
    `, 
    directives: [Form] 
}) 
export class App { 
    constructor() { 
    this.name = 'Angular2' 
    } 

    submitted(){ 
    console.log('clicked'); 
    } 
} 

frm.ts

import {Component, EventEmitter} from 'angular2/core' 

@Component({ 
    selector: 'frm', 
    providers: [], 
    template: ` 
    <div> 
     <h2>Form</h2> 
     <button (click) = "clicked()">Go</button> 
    </div> 
    `, 
    directives: [], 
    outputs: ['submitted', 'canceled'] 
}) 
export class Form { 

    submitted: EventEmitter<any> = new EventEmitter<any>(); 

    canceled: EventEmitter<any> = new EventEmitter<any>(); 

    constructor() { 

    } 

    clicked(){ 
    this.submitted.emit(null); 

    } 
} 

如果我刪除了事件綁定,它會工作。就像這樣:

<frm></frm> 

這裏是我的package.json:

{ 
    "name": "angular2-quickstart", 
    "version": "1.0.0", 
    "scripts": { 
    "postinstall": "npm run typings install", 
    "tsc": "tsc", 
    "tsc:w": "tsc -w", 
    "lite": "lite-server", 
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ", 
    "typings": "typings" 
    }, 
    "license": "ISC", 
    "dependencies": { 
    "angular": "~1.5.0", 
    "angular-animate": "~1.5.0", 
    "angular-aria": "~1.5.0", 
    "angular-material": "~1.0.5", 
    "angular-ui-bootstrap": "~1.1.2", 
    "angular2": "2.0.0-beta.16", 
    "es6-promise": "^3.0.2", 
    "es6-shim": "^0.35.0", 
    "jquery": "~2.2.0", 
    "ng2-bootstrap": "~1.0.3", 
    "reflect-metadata": "0.1.2", 
    "rxjs": "5.0.0-beta.2", 
    "systemjs": "0.19.26", 
    "zone.js": "0.6.12" 
    }, 
    "devDependencies": { 
    "concurrently": "^2.0.0", 
    "lite-server": "^2.2.0", 
    "typescript": "^1.8.10", 
    "typings": "^0.8.1" 
    } 
} 
+0

現在有beta.17。此外,也許你應該更新你的其他依賴關係。 – acdcjunior

+0

不能更新爲17,因爲ng2-bootstrap仍然不在17中。我不期待17會解決這個問題。 –

回答

0

問題是我缺少implements關鍵字。

+0

你怎麼解決這個問題? –

+0

正如我所提到的,我錯過了我的組件上的implements關鍵字來實現一個類。我加了這個,它工作。 –

1

似乎有一對夫婦的錯誤在你的app.js文件:

(1)聲明name變量在在訪問它之前,類的頂部,如:name:string;

(2)導入應該是這樣的:import {Form} from './frm'

+0

他們不是真正的錯誤。請記住,如果我刪除了我的活動訂閱,則一切正常 –

+0

以上提到的所有建議對我來說一切正常。我在beta 16上測試過。 – siva636

+0

我用Package.json文件更新了我的問題。如果我缺少升級內容,你是否介意檢查這個匹配? –

3

我認爲這個問題可能與你的輸出實例有關。這裏是做正確的方式(一個從你的工作pkunkr):

submitted: EventEmitter<any> = new EventEmitter<any>(); 

根據錯誤,Angular2不能在他們訂閱,因爲他們是不確定的。你確定他們在旅遊本地代碼中以相同的方式實例化嗎?

+0

我的腦子一分鐘後,這幫助了我。感謝您的快速,簡潔的解釋! –

相關問題