Angular 4:构建生产环境:属性是私有的,只能在类内部访问

14
我正在使用 Angular 4,运行命令:ng build --prod 我遇到了这个问题:
ng build --prod
Your global Angular CLI version (1.2.2) is greater than your local
version (1.0.0). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
Hash: 7fce5d10c4c3ac9745e8
Time: 68351ms
chunk    {0} polyfills.7790a64cc25c48ae62ea.bundle.js (polyfills) 177 kB {4} [initial] [rendered]
chunk    {1} main.f10680210e9e45ed33cc.bundle.js (main) 382 kB {3} [initial] [rendered]
chunk    {2} styles.d2e6408caea42ccabf99.bundle.css (styles) 175 bytes {4} [initial] [rendered]
chunk    {3} vendor.fc69ec31f7ef40b5fffb.bundle.js (vendor) 5.9 MB [initial] [rendered]
chunk    {4} inline.91747411075ce6c7f438.bundle.js (inline) 0 bytes [entry] [rendered]

ERROR in ng:///D:/Sources/DotNet/NextGCClientWeb/src/app/login/login.component.html (11,3): Property 'activatedRoute' is private and only accessible within class 'Login
Component'.

ERROR in ng:///D:/Sources/DotNet/NextGCClientWeb/src/app/login/login.component.html (11,3): Property 'activatedRoute' is private and only accessible within class 'Login
Component'.

这个错误看起来很奇怪:
ERROR in 
    ng:///D:/Sources/DotNet/NextGCClientWeb/src/app/login/login.component.html (11,3): Property 'activatedRoute' is private and only accessible within class 'Login Component

错误提示中指出的组件是以下内容:
import { Component } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Http, Response } from '@angular/http';
import { SessionService } from './../../shared/service';

import { User } from './../../model';

@Component({
  selector: 'login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
  error: string;
  email: string;
  password: string;
  stayConnected: Boolean;

  constructor (
    private sessionService: SessionService,
    private router: Router,
    private activatedRoute: ActivatedRoute
  ) {
    if (activatedRoute.snapshot.params['keyWording']) {
      this.sessionService.logInWithFinalization(activatedRoute.snapshot.params['keyWording']);
    }
  }

  logIn() {
    this.sessionService.logIn(this.email, this.password, this.stayConnected).subscribe(
      () => {
        if (this.sessionService.pageRequestedInUnauthenticated == null) {
          this.router.navigate(['/welcome']);
        } else {
          this.router.navigate([this.sessionService.pageRequestedInUnauthenticated]);
        }
      },
      (error: Response) => this.error = error.json()
    );
  }
}

HTML视图:
<div id="divLogin">
  <h1>Se connecter</h1><br>
  <i class="fa fa-envelope-o fa-lg" id="iconEmail"></i>
  <input type="text" id="email" [(ngModel)]="email" (keyup.enter)="logIn()" class="form-control" placeholder="Adresse email" autofocus />
  <i class="fa fa-lock fa-lg" id="iconPassword"></i>
  <input type="password" id="password" [(ngModel)]="password" (keyup.enter)="logIn()" class="form-control" placeholder="Mot de passe" />
  <button (click)="logIn()" class="btn btn-primary btn-block btn-lg">Connexion</button>
  <span id="containerStayConnected" title="Non implémenté"><input type="checkbox" id="stayConnected" [(ngModel)]="stayConnected" /><label for="stayConnected">Restez connecté</label></span>
  <a id="forgetPassword" title="Non implémenté">Mot de passe oublié</a><br><br><br>
  <a routerLink="/inscription">S'inscrire</a><br>
  {{ error }}
</div>

<div class="confirmationMessage" *ngIf="activatedRoute.snapshot.params['keyWording'] == 'validateInscription'"><br>Un lien de confirmation vous a été envoyé sur votre boîte email afin de valider votre compte. Merci.</div>

<div id="piedDePage"></div>

当运行 ng serve 时,我没有捕获到它。 有什么建议吗?

1
@firaskoubaa,请将 private activatedRoute 更改为 public activatedRoute。这样也许可以解决您的问题。 - Darshita
属性 activatedRoute 被定义为 private,使用 private activatedRoute: ActivatedRoute - martin
4个回答

18

将activatedRoute改为public

你需要将你的activatedRoute属性设置为public。以下是一个AOT编译时构建生产环境的清单(摘自此Github项目https://github.com/asadsahi/AspNetCoreSpa,但适用于任何其他Angular项目)。

AOT - Ahead of time compilation的注意事项:

  • 不要在模板或样式中使用require语句,而应该使用styleUrls和templateUrls。angular2-template-loader插件会在构建时将其更改为require。
  • 不要使用默认导出。

  • 不要使用form.controls.controlName,而应该使用form.get('controlName')

  • 不要使用control.errors?.someError,而应该使用control.hasError('someError')

  • 不要在提供程序、路由或声明中使用函数,应该导出一个函数,然后引用该函数名。输入、输出、视图或内容子级、Hostbindings以及从模板中使用或为Angular注释的任何字段都应该是public

保持activatedRoute私有

如果要保持你的activatedRoute属性私有,可以像这样做:

theData:any;
constructor (
private sessionService: SessionService,
private router: Router,
private activatedRoute: ActivatedRoute
 ) {
     this.activatedRoute.params.subscribe(
      params => {
        this.theData= params['keyWorking']; 
        //you will bind the theData instead of the activatedROute
      }
    );
}

在你的模板中

并且在你的模板中

<div class="confirmationMessage" *ngIf="theData == 'validateInscription'">
<br>Un lien de confirmation vous a été envoyé sur votre boîte email afin de 
 valider votre compte. Merci.</div>

但是为什么这种行为不一致呢?我的视图中的私有成员的一部分通过了验证,而另一部分却抛出了异常?它们之间有什么区别? - Gil Epshtain
如果你只是在开发模式下运行(ng serve),那么它会通过,但如果你要启用AOT构建生产环境,它将无法通过。 - brijmcq
这不是我的问题,为什么在 ng build 中,我的私有成员可以在HTML中访问,而其他的会抛出错误。 - Gil Epshtain

3
请查看此链接获取更详细的信息。
对于版本2、4、5,原始“错误”报告并非错误,而是AOT的正常限制。模板中使用的变量需要声明为“public”。模板被视为一个单独的TypeScript类。
如果你的组件变量在模板中使用,可以将它们全部从私有(private)改为公有(public)。
或者你可以使用ng build -prod --aot=false命令进行构建。

0

您正在尝试访问已注入到构造函数中作为私有变量的activatedRoute。这意味着在使用AOT时,您将无法从模板中访问它。

要解决此问题,请将其更改为public activatedRoute或完全不要从模板中使用它。


0
尝试使用“ng build -env=prod”命令。这个方法对我来说解决了这个错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接