为什么角分量场应该是公共的 [英] Why angular component field should be public

查看:67
本文介绍了为什么角分量场应该是公共的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此角度组件在开发模式下工作正常:

This angular component works fine on development mode:

这是html文件:

     <form>
            <select id="id_liste_selectionnee" [(ngModel)]="champ_id_liste_selectionnee" name="zzz">
                <option></option>
                <option *ngFor="let item of donnees" value="{{ item.id_liste }}">{{ item.nom_liste }}</option>
            </select>
        </div>
    </form>

这是打字稿文件的顶部:

And here is the top of the typescript file:

import { Component } from '@angular/core';
import {Service1} from "../services/service1.service";
import {Liste} from "../models/Liste";

@Component({
  selector: 'tagcomposant1',
  templateUrl: './composant1.component.html'
})
export class Composant1Component {
    private donnees:Liste[];
    private champ_id_liste_selectionnee:number|null;
  ....

但是当我尝试发布时请查看此错误:

But look at this error when i try to publish:

$ ng build --extract-css"--prod"

$ ng build --extract-css "--prod"

Property 'donnees' is private and only accessible within class 'Composant1Component'.

如果我将此字段更改为公开字段:一切正常.我的问题是:为什么我在开发模式下没有错误.该应用程序在我的开发计算机上的运行时运行良好.

If i changed this field to public: Everything works. My question is: Why do not i have an error in development mode. The application works very well at runtime on my development computer.

谢谢

推荐答案

在开发模式下,将使用Angular的JIT编译器:浏览器下载应用程序后,它将html模板转换为JavaScript代码.在JavaScript中,所有内容都是公开的,因此您没有问题.

In dev mode, the JIT compiler of Angular is used: once the application is downloaded by the browser, it transforms html templates into JavaScript code. In JavaScript, everything is public, so you have no problem.

在生产模式下,使用AOT编译器.构建应用程序时,模板将转换为TypeScript文件,然后将其与您编写的组件代码一起编译为JavaScript.

In prod mode, the AOT compiler is used. When building the application, the templates are transformed to TypeScript files, which are then compiled, along with the component's code you wrote, into JavaScript.

因此Angular为您的模板生成一个TypeScript类,当然需要访问您组件中的字段.由于它们是私有的,因此TypeScript编译器会抱怨.

So Angular generates a TypeScript class for your template, which of course needs to access the fields in your component. Since they are private, the TypeScript compiler complains.

即使在开发过程中,AOT有时也会成为默认设置.发生这种情况时,您会更快发现此类错误,并且不断发展.

AOT will, one day or another, become the default, even during development. When that happens, you will detect such mistakes sooner, whilde developing.

这篇关于为什么角分量场应该是公共的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆