相关疑难解决方法(0)

我应该如何在Angular 8中为@ViewChild使用新的static选项?

我应该如何配置新的Angular 8视图子级?

@ViewChild('searchText', {read: ElementRef, static: false})
public searchTextInput: ElementRef;
Run Code Online (Sandbox Code Playgroud)

@ViewChild('searchText', {read: ElementRef, static: true})
public searchTextInput: ElementRef;
Run Code Online (Sandbox Code Playgroud)

哪个更好?什么时候应该使用static:truevs static:false

typescript viewchild angular angular8

125
推荐指数
4
解决办法
3万
查看次数

“HTMLElement”类型上不存在属性“getContext”

我正在 Angular8 中做一个项目并使用 ChartJS 创建图表。运行项目后,它将显示图表。但它会给出一个错误说:

Property 'getContext' does not exist on type 'HTMLElement'.
Run Code Online (Sandbox Code Playgroud)

我如何摆脱这个错误?

这是我的代码:

图表.component.html

<div id="container" style="width:350px;height:250px">
  <canvas id="myChart" width=300 height=300></canvas>   
</div>
Run Code Online (Sandbox Code Playgroud)

图表组件.ts

import {Component, OnInit} from '@angular/core';
import {Chart} from 'chart.js';
import {ChartService} from '../charts/chart.service';
import {HttpClient} from '@angular/common/http';

// import {DATA} from '../CHART/CHARTS.MODEL';

@Component({
  selector: 'app-charts',
  templateUrl: './charts.component.html',
  styleUrls: ['./charts.component.scss']
})
export class ChartsComponent implements OnInit {

  constructor(
    private ChartItems: ChartService,
    private httpClient: HttpClient
  ) {   }

  ngOnInit() {
    this.getChartDetails();
}

  getChartDetails() {
    this.ChartItems.getChart().subscribe(data => { …
Run Code Online (Sandbox Code Playgroud)

chart.js angular

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

如何使用 new 关键字实例化角度分量?

如何使用 new 关键字实例化角度分量?

想象一下以下作为无模板组件:

import { Component } from '@angular/core';

@Component({
  selector: 'my-component',
})
export class MyComponent {
constructor(private myService: MyService)
}

Run Code Online (Sandbox Code Playgroud)

我知道角度组件仅在模板中找到选择器标签时才会实例化。

但是...我如何使用 new 关键字实例化上面的内容?因为我的组件不需要模板。

但它应该是一个带有@component装饰器的角度组件。(因为我需要依赖注入的功能)

但问题是,当我创建一个如下所示的新组件时:

const comp = new MyComponent()
Run Code Online (Sandbox Code Playgroud)

它还要求在实例化时将服务实例传递给其构造函数。像这样”

const comp = new MyComponent(new MyService())
Run Code Online (Sandbox Code Playgroud)

当我们再次传入的服务依赖于其他服务时,事情会变得更加困难。

像这样...

const comp = new MyComponent(new MyService(new AnotherService(new YetAnotherService(... so on))))
Run Code Online (Sandbox Code Playgroud)

那么有解决方法吗?

Angular 的 DI 很好。因为我们没有像上面这样的问题。它会自动创建所有注入的服务。那么用new关键字可以吗?

注意我尝试使用普通的打字稿类。但我还是不喜欢它。我想使用角度组件本身。

那可能吗?

angular

5
推荐指数
1
解决办法
1216
查看次数

标签 统计

angular ×3

angular8 ×1

chart.js ×1

typescript ×1

viewchild ×1