속성 'X'는 비공개이며 'xyzComponent'클래스 내에서만 액세스 할 수 있습니다.
이 블로그 를 팔로우하고있는 프로덕션 용 angular2 애플리케이션을 구축하려고합니다 . 내 ngc가 성공적으로 컴파일되면 tsc 컴파일 이 수행되면 이미지에 표시된 오류가 생성됩니다.
After searching for a while I found this blog which explains the problem in "The context property" section which I'm not able to understood properly may be it give some good idea to you that what's happening wrong. basically when we're making a variable private we're getting "ERROR: Property is private and only accessible within class". I'm not understanding why is it coming.
Kindly help us as we're banging our head in this problem for past couple of days.
For a given component all its members (methods, properties) accessed by its template must be public in the AOT compilation scenario. This is due to the fact that a template is turned into a TS class. A generated class and a component are 2 separate classes now and you can't access private members cross-class.
In short: you can't access private members in your templates if you want to use ahead-of-time compilation.
For better explaination https://github.com/angular/angular/issues/11422
Maybe another even simpler answer is:
Guys don't call private methods, fields or properties from the HTML :)
P.S. when compiling the *.ts
code to *.js
, AOT refuse to connect non-public members with the HTML template.
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.
I got this when I declared private injectables in the constructor:
constructor(private service: SpecificObjectService) { }
And used them in the template:
*ngFor="let pd of service.listSpecificObject "
The solution is:
constructor(public service: SpecificObjectService) { }
이것은 나를 위해 작동합니다. 단순히 서비스를 공개로 변경하십시오.
constructor(public service: SpecificObjectService) { }
생산중인 앱 !!
'Program Tip' 카테고리의 다른 글
TypeScript : 인터페이스 대 클래스 대 모듈 대 프로그램 대 함수 (0) | 2020.11.30 |
---|---|
부트 스트랩 클래스의 전체 목록 (0) | 2020.11.30 |
C 및 C ++에서 세미콜론 대신 쉼표를 사용하는 경우의 효과 (0) | 2020.11.30 |
PHP에서 객체 연산자“->”를 어디에 사용합니까? (0) | 2020.11.30 |
Android는 내 인 텐트 Extras를 계속 캐싱합니다. 새로운 추가 항목을 유지하는 보류중인 인 텐트를 선언하는 방법은 무엇입니까? (0) | 2020.11.30 |