相关疑难解决方法(0)

Angular组件中的"私有"和"公共"

如果我不加私人之前foo,loadBartext,我相信他们是公共默认情况下.

export class RandomComponent {
  @Input() foo: string;
  @Output() loadBar = new EventEmitter();
  text: string;
}
Run Code Online (Sandbox Code Playgroud)

当它们public在组件中时是否有任何用例?

出于封装/安全原因,我是否应该private像以下一样为所有这些添加?

export class RandomComponent {
  @Input() private foo: string;
  @Output() private loadBar = new EventEmitter();
  private text: string;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

typescript angular

103
推荐指数
2
解决办法
6万
查看次数

Angular 2 Ahead-of-Time编译器:我必须公开所有类属性吗?

Angular 2 rc 6,typescript 2,node 4.5.0,npm 2.15.9Windows 7

我正试图从即时编译转变为提前编译,我依赖这些资源:

我知道我需要运行编译器ngc来生成ngfactory.ts文件,我需要更改main.ts为使用platformBrowser而不是platformBrowserDynamic引导程序.我遇到了障碍,但不知道如何继续.

1.我已确认使用即时编译可以正常运行App.我main.ts是:

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)

2.我清除了生产文件夹中的所有应用程序文件,但保留了第三方库(例如:Angular 2,Angular 2 Material)

3.我运行"node_modules/.bin/ngc" -p ./它运行时没有输出到控制台.我看到了.ngfactory.ts每个.ts组件和模块的文件.我还看到了.css.shim.ts每个.css持有组件样式的文件.此外,.js.js.map文件已被transpiled,并放置在生产目录

4.如果我此时尝试运行应用程序,我会看到包含组件模板的404 not found所有.html文件的错误

5.我手动将所有模板文件(.html)移动到生产目录并运行应用程序.它运行正常,但它仍然使用即时编译(255个请求,包括compiler.umd.js)

6.我改变main.ts为: …

deployment npm typescript angular2-aot angular

20
推荐指数
1
解决办法
5719
查看次数

属性'http'在类型'Component',Angular 2上不存在

我是angular2和打字稿的新手,已经花了半天的时间来弄清楚ng2表格.我已完成所有路线并构建了所有必要的表格,目前正在尝试了解如何使用打字稿在angular2中发布

这是我的错误:

[default]中的错误simpleapp/src/app/clients/add-client/add-client.component.ts:52:16属性'http'在'AddClientComponent'类型中不存在.

而且我找不到这个问题来自哪里.我已经angular/http在我的组件中导入了,我提供了标题和响应作为官方教程说但仍然可以看到这个问题.我错过了什么,我的问题在哪里?提前致谢

这是我的组成部分:

import 'rxjs/add/operator/map';

import {Component} from '@angular/core';
import {Http, Response, RequestOptions, Headers} from '@angular/http';

import {Employee} from "./model/add-client.model";

@Component({
  selector: 'app-add-client',
  templateUrl: 'add-client.component.html',
  styleUrls: ['add-client.component.css']
})

export class AddClientComponent {

   departments: Array<any>;
   firstName: '';
   lastName: '';
   id: null;
   salary: null;
   phone: null;
   departmentId: null;

  constructor(http: Http) {
    http.get('api/departments')
      .map((res: Response) => res.json())
      .subscribe((departments: Array<any>) => this.departments = departments);
  }

  model = new Employee(
    this.id,
    this.firstName,
    this.lastName,
    this.salary,
    this.departmentId,
    this.phone
  );

  submitted = …
Run Code Online (Sandbox Code Playgroud)

forms typescript angular2-http angular

13
推荐指数
1
解决办法
2万
查看次数

用玩笑测试打字稿中的私有函数

在下面的代码中,我的测试用例按预期通过,但我使用 stryker 进行突变测试,handleError 函数在突变测试中幸存下来,所以我想通过测试是否调用 handleError 函数来杀死突变体。需要帮忙测试私有函数。

我试过 spyOn 但没有用

const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)

const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)

typescript jestjs nestjs

11
推荐指数
1
解决办法
2万
查看次数

公共或私有 - Angular 2组件类方法混淆

我很难理解哪些方法应该是私有的,哪些方法应该在组件类中公开.

在服务中判断方法是公共的还是私有的似乎相当容易,例如:

export class MyServiceClass {
  private _cache = {}; // this value is private and shouln't be accessed from outside
  public accessCache(){ // it's public as it's an API method
    return this._cache;
  }
  public setCache(newVal){
     this._cache = newVal;
  }
}
Run Code Online (Sandbox Code Playgroud)

遵循该逻辑,组件中的所有方法都应该是私有的,因为这些方法都不应该暴露在类之外.(根据该帖子组件及其视图是一个实体)

export class MyComponent {      
  private _getRandomNumbers(){ // this is used in view only
    /*..*/
  }
}
Run Code Online (Sandbox Code Playgroud)

没有悲剧,但是在本视频中你可以了解到只有组件的公共方法才能进行单元测试.通过上面的命令,我找不到任何理由在组件类中使用公共方法,但我仍然有一些值得测试的方法(特别是在视图中使用的方法).这意味着我完全失去了角度世界中私人和公共方法的含义.

所以我的问题很简单:

组件中的哪些方法应标记为公共和私有.

typescript angular

8
推荐指数
1
解决办法
5355
查看次数