2016-12-05 5 views
1

Я использовал Angular-cli для начальной настройки кода. Мне нужно интегрировать Угловая 2: Toastr библиотека, но некоторые, как я не могу использовать. Он отлично работал, когда я использовал без формата Angular-cli. Я получаю следующую ошибку. когда я выполняю код тоста.Angular2: Невозможно прочитать свойство 'vcRef' of undefined

error_handler.js:47 EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'vcRef' of undefined 
TypeError: Cannot read property 'vcRef' of undefined 
at http://localhost:4200/main.bundle.js:42306:101 
at new ZoneAwarePromise (http://localhost:4200/main.bundle.js:63592:29) 
at ToastsManager.show (http://localhost:4200/main.bundle.js:42297:16) 
at ToastsManager.success (http://localhost:4200/main.bundle.js:42395:21) 
at AppComponent.showSuccess (http://localhost:4200/main.bundle.js:41105:21) 
at CompiledTemplate.proxyViewClass.View_AppComponent0.handleEvent_0 (/AppModule/AppComponent/component.ngfactory.js:34:34) 
at CompiledTemplate.proxyViewClass.<anonymous> (http://localhost:4200/main.bundle.js:52326:37) 
at HTMLButtonElement.<anonymous> (http://localhost:4200/main.bundle.js:27715:36) 
at ZoneDelegate.invokeTask (http://localhost:4200/main.bundle.js:63339:35) 
at Object.onInvokeTask (http://localhost:4200/main.bundle.js:25786:37) 

Я выполнение следующего кода,

import {Component} from "@angular/core"; 
import { ToastsManager } from 'ng2-toastr/ng2-toastr'; 

@Component({ 
    selector: 'app-root', 
    template: '<button class="btn btn-default" (click)="showSuccess()">Toastr Tester</button>' 
}) 
export class AppComponent { 

    constructor(public toastr: ToastsManager) { 
    } 

    showSuccess() { 
    this.toastr.success('You are awesome!', 'Success!'); 
    } 
} 

Угловой версия 2.2.1

Спасибо заранее.

ответ

7

Согласно NG2-toastr документации

Рассекая решение об изменении углового v2.2.x

// AppComponent.ts (Root component of your app) 

constructor(public toastr: ToastsManager, vRef: ViewContainerRef) { 
    this.toastr.setRootViewContainerRef(vRef); 
} 

https://github.com/PointInside/ng2-toastr#breaking-change-solution-for-angular-v22x

NG2-toastr использует динамически компонент создания с помощью vcRef.createComponent и требует a ViewContainerRef ссылка. Но есть только два пути для доступа к ViewContainerRef:

  • вы можете поместить директиву впрыскивается с ViewContainerRef на элемент,
  • или получить его с помощью запроса ViewChild.

https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html

Таким образом, вы должны внедрить это в корневой компонент.

Примечание: без линии:

this.toastr.setRootViewContainerRef(vRef); 

он должен работать, а потому, что NG2-toastr имеет небольшой хак, как:

if (!this._rootViewContainerRef) { 
    this._rootViewContainerRef = this.appRef['_rootComponents'][0]['_hostElement'].vcRef; 
} 

для доступа к корневой viewContainerRef

https://github.com/PointInside/ng2-toastr/blob/72a35d01fa4a7c67395b3ada5c74124b1701697f/src/toast-manager.ts#L46-L48

+0

Работая спасибо. –

Смежные вопросы