angular - 类型 'undefined' 不可分配给类型 'ApexAxisChartSeries | ApexNonAxisChartSeries'

标签 angular typescript charts apex apexcharts

我在ts中用过这段代码

export type pieChartOptionss = {
  series?: ApexNonAxisChartSeries | '';
  chart?: ApexChart | '';
  legend?: ApexLegend | '';
  dataLabels?: ApexDataLabels | '';
  responsive?: ApexResponsive[] | '';
  labels?: any | '';
};
export class DashboardComponent implements OnInit {
 public pieChartOptionss!: Partial<pieChartOptionss>;
}
     (response: any) => {
          this.user_data = response.results;
          console.log('user_data', this.user_data)
          this.user_data_true = this.user_data.filter(x => x.is_active === true)
          this.user_data_false = this.user_data.filter(x => x.is_active === false)
          this.pieChartData = [this.user_data_true.length, this.user_data_false.length];
          this.pieChartOptionss = {
            series: [this.user_data_true.length, this.user_data_false.length],
            chart: {
              type: 'donut',
              width: 270,
            },
            legend: {
              show: false,
            },
            dataLabels: {
              enabled: true,
            },
            labels: ['Active', 'Inactive'],
            responsive: [
              {
                breakpoint: 480,
                options: {},
              },
            ],
          };
        }

在 html 中我使用了这段代码:

    <div class="body" *ngIf="pieChartOptionss">
          <apx-chart [series]="pieChartOptionss?.series" [chart]="pieChartOptionss?.chart"
            [labels]="pieChartOptionss?.labels" [responsive]="pieChartOptionss?.responsive"
            [dataLabels]="pieChartOptionss?.dataLabels" [legend]="pieChartOptionss?.legend" class="apex-pie-center">
          </apx-chart>
          <div class="table-responsive m-t-15">
            <table class="table align-items-center">
              <tbody>
                <tr>
                  <td><i class="fa fa-circle col-cyan mr-2"></i> {{ "active" | translate }}</td>
                  <td class="col-blue">{{pieChartOptionss?.series[0]}}</td>
                </tr>
                <tr>
                  <td><i class="fa fa-circle col-green mr-2"></i>{{ "inactive" | translate }}</td>
                  <td class="col-green">{{pieChartOptionss?.series[1]}}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>

问题是,当我为项目提供服务时显示此错误:

ERROR in src/app/panel/dashboard/dashboard.component.html:97:26 - error TS2322: Type '"" | ApexNonAxisChartSeries | undefined' is not assignable to type 'ApexAxisChartSeries | ApexNonAxisChartSeries'.
Type 'undefined' is not assignable to type 'ApexAxisChartSeries | ApexNonAxisChartSeries'.

97 <apx-chart [series]="pieChartOptionss?.series" [chart]="pieChartOptionss?.chart" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:97:62 - error TS2322: Type '"" | ApexChart | undefined' is not assignable to type 'ApexChart'. Type 'undefined' is not assignable to type 'ApexChart'.

97 <apx-chart [series]="pieChartOptionss?.series" [chart]="pieChartOptionss?.chart" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:98:53 - error TS2322: Type '"" | ApexResponsive[] | undefined' is not assignable to type 'ApexResponsive[]'. Type 'undefined' is not assignable to type 'ApexResponsive[]'.

98 [labels]="pieChartOptionss?.labels" [responsive]="pieChartOptionss?.responsive" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:99:17 - error TS2322: Type '"" | ApexDataLabels | undefined' is not assignable to type 'ApexDataLabels'. Type 'undefined' is not assignable to type 'ApexDataLabels'.

99 [dataLabels]="pieChartOptionss?.dataLabels" [legend]="pieChartOptionss?.legend" class="apex-pie-center"> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:99:61 - error TS2322: Type '"" | ApexLegend | undefined' is not assignable to type 'ApexLegend'. Type 'undefined' is not assignable to type 'ApexLegend'.

99 [dataLabels]="pieChartOptionss?.dataLabels" [legend]="pieChartOptionss?.legend" class="apex-pie-center"> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:106:46 - merror TS2532: Object is possibly 'undefined'.

106 {{pieChartOptionss?.series[0]}} ~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/app/panel/dashboard/dashboard.component.ts:93:16 93 templateUrl: './dashboard.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component DashboardComponent. src/app/panel/dashboard/dashboard.component.html:110:47 - error TS2532: Object is possibly 'undefined'.

110 {{pieChartOptionss?.series[1]}}

最佳答案

唯一对我有用的解决方案是添加单词“any”,如下面的代码所示:

public pieChartOptionss!: Partial<pieChartOptionss> | any;

关于angular - 类型 'undefined' 不可分配给类型 'ApexAxisChartSeries | ApexNonAxisChartSeries',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66886287/

相关文章:

angular - 只有常量的 Typescript 类

javascript - 是否可以在 Angular 4 中的这两个索引之间设置两个索引 & ngFor

javascript - 谷歌线图滚动/缩放问题

excel - 如何使用 VBA 自动扩展图表的范围?

r - 用于可视化离散和连续面板数据的加权桑基/冲积图?

angular - NativeScript 找不到 Angular 组件

javascript - 在未完成时缓存 Rxjs http 请求的最短代码?

typescript - 具有angular2的materialize-css中的多个选择不呈现动态值

typescript - Aurelia StageComponent - 如何将子组件渲染到 DOM 树中?

Angular 6 更新 Observable