小编The*_*224的帖子

如何声明一个在Typescript中引发错误的函数

在Java中,我将这样声明一个函数:

public boolean Test(boolean test) throws Exception {
  if (test == true)
    return false;
  throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)

而且我可以使用此功能而无需处理异常。

如果可能,如何在Typescript中执行相同的操作?编译器会告诉我,如果没有try / catch,我将无法使用该函数。

typescript

16
推荐指数
3
解决办法
7444
查看次数

将 ng-content 传递给子组件

谢谢你的时间。

我有三个组成部分:

  1. 主页面组件
  2. 一个按钮谁使用 angular/cdk/overlay overlay.create用于创建第三个组件的
  3. 附在显示文本的按钮上的小框

目标是在用户单击按钮时显示有关页面的信息。我不能使用@Input,因为格式 ( h1,p信息,分量)将为每个页面更改。

我的问题

如何将 HTML 从主组件传递到小框组件?

或者

我怎样才能截取内容 ng-content并将其发送到小盒子组件?

主要的

<app-btn-info>
   <mat-icon>info</mat-icon>
   <h1>Test</h1>
   <p>This is a test</p>
</app-btn-info>
Run Code Online (Sandbox Code Playgroud)

按钮

@Component({
  selector: 'app-btn-info',
  templateUrl: './btn-info.component.html',
  styleUrls: ['./btn-info.component.scss']
})
export class BtnInfoComponent {

  @ViewChild('button') button: MatIcon;

  constructor(private overlay: Overlay) { }

  public onClick() {
    this.overlay.create({
      positionStrategy: this.overlay.position().connectedTo(this.button._elementRef,
        { originX: 'start', originY: 'top' }, { overlayX: 'start', overlayY: 'top' }
      )
    });
  }

}
Run Code Online (Sandbox Code Playgroud)
<button #button mat-icon-button color="warn" …
Run Code Online (Sandbox Code Playgroud)

html javascript typescript angular-material2 angular

9
推荐指数
1
解决办法
1562
查看次数

如何在父类中注入服务而不将其传递到构造函数中?角6

我想知道如何在抽象类中注入具有多个依赖项的服务,该抽象类将由多个类扩展。

以更有效的方式将其传递到所有构造函数中!

我尝试创建静态,但如果该服务从未被另一个服务实例化,则永远不会分配单例实例变量

像这样的:(只是一个例子)

@Injectable({
  providedIn: 'root'
})
export class AnimalService {

  constructor(private http: HttpClient, private userService: UserService) {}

  countTotalInDB(type): number {
    return this.http.get(...);
  }

  getUserAnimals(userId: number) {
    return this.userService.getUser(userId).animals;
  }

}

abstract class Animal {

  constructor() {}

  public getTotalInDataBase(type): number {
    // How to get a instance of AnimalService ?
    return animalService.countTotalInDB(type);
  }

}

export class Cat extends Animal {

  constructor() {
    super();
  }

  public getTotalInDataBase(): number {
    return super.getTotalInDataBase('cat');
  }

}

export class Dog extends Animal {

  constructor() …
Run Code Online (Sandbox Code Playgroud)

dependency-injection typescript angular

3
推荐指数
1
解决办法
1899
查看次数

如何映射到数组并使用tslint返回对象。(句法糖)

这是一个纯粹的句法糖问题。

如何在map不使用TSLint的情况下使用遍历数组并返回新对象:

可以通过省略花括号和关键字“ return”,并将对象文字括在括号中来简化此箭头函数的主体。

例如,对象用户:

class User {
    constructor(
        public id: number, 
        public first_name: string, 
        public last_name: string, 
        public gender: Date, 
        public location: number, 
    )
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时:

const simple_users = users.map(u => { return { name: u.name, id: u.id} });
Run Code Online (Sandbox Code Playgroud)

然后发生这种情况:

[tslint]通过省略花括号和
关键字“ return”,并将对象文字括在括号中。(箭头返回简写)

而且我要遵守tslint规则arrow-return-shorthand

typescript tslint angular

0
推荐指数
1
解决办法
1112
查看次数