简体   繁体   中英

Angular2 : Cannot read property 'vcRef' of undefined

I had used Angular-cli for the initial code setup. I need to integrate Angular 2: Toastr library but some how i'm not able to use. It was working fine when i had used without Angular-cli format. I'm getting following error. when i execute the toast code.

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)

I'm executing following code,

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!');
  }
}

Angular version 2.2.1

Thanks in advance.

According to ng2-toastr documentation

Breaking change solution for Angular 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 uses dynamically creation component via vcRef.createComponent and it requires a ViewContainerRef link. But there are only two ways to access a ViewContainerRef :

  • you can either place a Directive injected with ViewContainerRef on the Element,
  • or you obtain it via a ViewChild query.

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

So you have to inject this in your root component.

Notice : without the line:

this.toastr.setRootViewContainerRef(vRef);

it should work as well because ng2-toastr has a small hack like:

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

to access root viewContainerRef

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM