Property 'X' is private and only accessible within class 'xyzComponent'

前端 未结 7 1247
逝去的感伤
逝去的感伤 2020-12-07 13:48

I\'m trying to build angular2 application for production for that I\'m following this blog. After my ngc successful compilation when the

7条回答
  •  清歌不尽
    2020-12-07 14:23

    So I fixed this problem I'll keep this short and simple. To fix this I read this blog deeply. As in section "The context property" The solution for this problem is that Don't use or create a private variable if you want to use it in the view directly when your are creating your build with AOT (i.e., Ahead Of Time) for production.

    *for Example *

    // component.ts
    @Component({
      selector: 'third-party',
      template: `
        {{ _initials }}
      `
    })
    class ThirdPartyComponent {
      private _initials: string;
      private _name: string;
    
      @Input()
      set name(name: string) {
        if (name) {
          this._initials = name.split(' ').map(n => n[0]).join('. ') + '.';
          this._name = name;
        }
      }
    }
    

    output: Property '_initials' is private and only accessible within class 'ThirdPartyComponent'.

    Solution:

    update this private _initials: string; to simply _initials: string;

    For this answer Harish Gadiya provide me some help so thanx for that.

提交回复
热议问题