【问题标题】:Error :'error TS2339: Property 'data' does not exist on type 'Response'.- Angular错误:'error TS2339:“响应”类型上不存在属性“数据”。- Angular
【发布时间】:2018-08-07 10:51:22
【问题描述】:

我是学习 Angular 6 的新手,执行以下代码时遇到错误。请任何人帮助我解决此问题。

我在浏览器控制台中遇到以下错误,请在下面找到文件供您参考。 错误:

Uncaught Error: Can't resolve all parameters for AppComponent: (?).
at syntaxError (compiler.js:1016)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (compiler.js:10917)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (compiler.js:10810)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:10429)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getEntryComponentMetadata (compiler.js:11013)
at compiler.js:10673
at Array.map (<anonymous>)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (compiler.js:10673)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (compiler.js:23846)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:23827)

下面是应用组件文件

app.Component.ts

        import { Component, Input, OnInit, OnDestroy  } from '@angular/core';
        import { Subscription,Observable } from 'rxjs';
        import { AppService } from './app.service';
        import { HttpClient } from '@angular/common/http';
        import { Chart } from 'angular-highcharts';
        import 'rxjs/add/operator/catch';
        @Component({
          selector: 'app-root',
          templateUrl: './app.component.html',
          styleUrls: ['./app.component.css']
        })
        export class AppComponent implements OnDestroy, OnInit  {
          title = 'my-project';
           @Input() showMePartially: boolean;
            options: any;
            data: number;
            chart: any;
            dataSubscription: Subscription;
            chartData$ : Object;
            constructor(public appService: AppService) {
               this.options = {
                chart: {  type: 'pie',
                         plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false,  },
           //     legend: { enabled: false },
                credits: { enabled: false },
                 tooltip: {
       //     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: false,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false,
             //      format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                }
              }
            },
                 series: [{
                  name: 'Dispo',
                  data: []
                }]
            };
        }

       // onPointSelect (e) {
         // this.point = e.context.y;
       // }

       saveInstance(chartInstance) {
        this.chart = chartInstance;
      //   console.log(chartInstance);
    }
     public ngOnInit () {
        this.dataSubscription = this.appService.getData().subscribe((data) => {
         69 LINE - this.options.series[0].data = data.data.operating_rate;
           // Code for the pie

            let percentUp = data.data.operating_rate; // 88.14
            let percentDown = 100 - percentUp; // 11.86
            this.options.series[0].data = [
            {
            name: 'Up',
            y: percentUp,
            color: 'green'
            },
            {
            name: 'Down',
            y: percentDown,
            color: 'white'
            }
            ]
          console.log(data);
       });
    }
        public ngOnDestroy() {
          if (this.dataSubscription) {
    this.dataSubscription.unsubscribe();
    }
   }
}

下面是应用组件文件 app.Component.html

<chart [options]="options">
      <series>
     </series>
  </chart>

app.Service.ts

import { Injectable } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {Observable} from 'rxjs';
import { timer } from 'rxjs/observable/timer';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/map';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/publishBehavior';

@Injectable({
  providedIn: 'root'
})

@Injectable()
export class AppService {

  data: any;
  appService: Observable<AppService[]>;

  constructor(public http: Http) { }

  getData(){
        /*const tick3 = Observable.timer(100, 60000); 
  this.appService = tick3.flatMap(() => http.get(usersURL)).map(res => [res.json()]).publishBehavior(<AppService[]>[]).refCount();*/
        return this.http.get('https://my-json-server.typicode.com/techsithgit/json-faker-directory/profiles');

    }
 }
const usersURL = 'https://my-json-server.typicode.com/techsithgit/json-faker-directory/profiles';

app.module.ts

 import { BrowserModule } from '@angular/platform-browser';
    import { NgModule,CUSTOM_ELEMENTS_SCHEMA  } from '@angular/core';
    import { HttpModule } from '@angular/http';
    import { AppComponent } from './app.component';
    import { ChartModule } from 'angular-highcharts';
    import * as highcharts from 'highcharts';


    @NgModule({
      declarations: [
        AppComponent

      ],
      imports: [
        BrowserModule,
        HttpModule,
        ChartModule

      ],
      providers: [],
      bootstrap: [AppComponent],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    })
    export class AppModule { }

控制台错误

Uncaught Error: Template parse errors:
Can't bind to 'options' since it isn't a known property of 'chart'. ("<!--The content below is only a placeholder and can be replaced.-->
<chart [ERROR ->][options]="options">
      <series>
     </series>
"): ng:///AppModule/AppComponent.html@1:7
'series' is not a known element:
1. If 'series' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("--The content below is only a placeholder and can be replaced.-->
<chart [options]="options">
      [ERROR ->]<series>
     </series>
  </chart>
"): ng:///AppModule/AppComponent.html@2:6
'chart' is not a known element:
1. If 'chart' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("<!--The content below is only a placeholder and can be replaced.-->
[ERROR ->]<chart [options]="options">
      <series>
     </series>
"): ng:///AppModule/AppComponent.html@1:0
    at syntaxError (compiler.js:1016)
    at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14813)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:23988)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:23975)
    at compiler.js:23918
    at Set.forEach (<anonymous>)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:23918)
    at compiler.js:23828
    at Object.then (compiler.js:1007)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:23827)

【问题讨论】:

  • 哪一行抛出数据错误?
  • @fireGloves 第 27 行 src/app/app.component.ts(69,42) 中的错误:错误 TS2339:“响应”类型上不存在属性“数据”。
  • 您能在此处在您的代码中签名吗?我在 stackoverflow 上没有行号 :) 请发布您的 json 响应
  • @firegloves 我已经在 app.component.ts 文件第 69 行添加了行号,请检查并收到错误而不是 json 响应,请在上面的问题中找到带有标签行控制台错误的问题跨度>
  • 我认为你的问题是这段代码:data.data。请发布你的 json 响应数据以查看它包含的内容

标签: angular charts highcharts angular6


【解决方案1】:

正如您在 AppService 中编写的代码:

getData: any;

通过这种方式,您只是声明了一个变量。要调用 getData 函数,您应该在 AppService 中添加类似这样的内容:

getData(): void {
   // DO SOMETHING
}

【讨论】:

  • 嗨 Firegloves 我已经尝试过了,但出现了上述错误。我已经更新了问题
【解决方案2】:

您是否尝试将Inject 指令添加到您的app.component.ts 文件中?下面是组件构造函数的代码:

constructor(@Inject(AppService) public appService: AppService)

app.component.ts 文件更改:

export class AppComponent implements OnDestroy, OnInit {
  title = 'my-project';
  @Input() showMePartially: boolean;
  options: any;
  data: number;
  chart: any;
  dataSubscription: Subscription;
  chartData$: Object;
  constructor(@Inject(AppService) public appService: AppService) {
    this.options = {
      chart: {
        type: 'pie',
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
      },
      //     legend: { enabled: false },
      credits: {
        enabled: false
      },
      tooltip: {
        //     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
      },
      plotOptions: {
        pie: {
          allowPointSelect: false,
          cursor: 'pointer',
          dataLabels: {
            enabled: false,
            //      format: '<b>{point.name}</b>: {point.percentage:.1f} %',
          }
        }
      },
      series: [{
        name: 'Dispo',
        data: []
      }]
    };
  }

不要忘记导入适当的指令:

 import { Component, Input, Inject, OnInit, OnDestroy  } from '@angular/core';

【讨论】:

  • 我正在尝试,完成后会通知您
  • 嗨丹尼尔,我在 src/app/app.component.ts(67,45) 中遇到错误错误:错误 TS2339:“AppService”类型上不存在属性“订阅”。在行 this.dataSubscription = this.appService.subscribe((data)
  • 发生这种情况是因为您没有在 app.service 中定义 subscribe() 函数,但这是另一个问题。我的回答解决了您的问题,所以请将其标记为答案。
猜你喜欢
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 2021-10-22
  • 2021-12-01
  • 1970-01-01
  • 2019-01-21
  • 2016-08-13
相关资源
最近更新 更多