“对象”类型上不存在属性“”

我整天都在工作。一整天一切正常。
重新启动服务器(ng serve)。现在突然有很多错误。
我设法解决了大部分问题,但我被这个问题困住了。

http://img.mukewang.com/61dfeb140001563409750506.jpg

这是组件 .ts 文件的主要部分:


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

import { HttpService } from '../http.service';


@Component({

  selector: 'app-playboard',

  templateUrl: './playboard.component.html',

  styleUrls: ['./playboard.component.scss']

})

export class PlayboardComponent implements OnInit {

  brews: Object;


  constructor(private _http: HttpService) { }


  ngOnInit() {

    this._http.myMethod().subscribe(data => {

      this.brews = data;

      this.dices = this.brews.myBox;

      this.diceSeed = this.brews.boxID;

      console.log(this.brews);

    });

  }

这是 http.service.ts 文件:


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

import {HttpClient } from '@angular/common/http';


@Injectable({

  providedIn: 'root'

})

export class HttpService {


  constructor(private http: HttpClient) { }


  myMethod() {

    return this.http.get<Object>('https://localhost:44398/api/boggle');

  }


  myWordMethod(word) {

    var url = 'https://localhost:44398/api/wordpoints/' + word;

    return this.http.get<Object>(url);

  }

}

它工作了一整天,突然出现这些奇怪的错误。

有谁知道可能出了什么问题?非常感谢!


婷婷同学_
浏览 1018回答 2
2回答

12345678_0001

从您的 http 调用中删除对象。使用泛型是可选的,尤其是在您没有输入回复的情况下。import { Injectable } from '@angular/core';import {HttpClient } from '@angular/common/http';@Injectable({&nbsp; providedIn: 'root'})export class HttpService {&nbsp; constructor(private http: HttpClient) { }&nbsp; myMethod() {&nbsp; &nbsp; return this.http.get('https://localhost:44398/api/boggle');&nbsp; }&nbsp; myWordMethod(word) {&nbsp; &nbsp; var url = 'https://localhost:44398/api/wordpoints/' + word;&nbsp; &nbsp; return this.http.get(url);&nbsp; }}在 brews 上,将其声明为 any:import { Component, OnInit } from '@angular/core';import { HttpService } from '../http.service';@Component({&nbsp; selector: 'app-playboard',&nbsp; templateUrl: './playboard.component.html',&nbsp; styleUrls: ['./playboard.component.scss']})export class PlayboardComponent implements OnInit {&nbsp; brews: any;&nbsp; constructor(private _http: HttpService) { }&nbsp; ngOnInit() {&nbsp; &nbsp; this._http.myMethod().subscribe(data => {&nbsp; &nbsp; &nbsp; this.brews = data;&nbsp; &nbsp; &nbsp; this.dices = this.brews.myBox;&nbsp; &nbsp; &nbsp; this.diceSeed = this.brews.boxID;&nbsp; &nbsp; &nbsp; console.log(this.brews);&nbsp; &nbsp; });&nbsp; }

暮色呼如

您可以通过简单地将它们定义为 any 类型来忽略它们。例如;myWordMethod(word: any) {&nbsp; &nbsp; ..}this._http.myMethod().subscribe(data: any => {&nbsp; &nbsp; ..});也就是说,通常首选为 TypeScript 声明实际类型。例如,如果您的 API 发回具有特定属性的通用对象,则将其声明为这样;interface MyMethodResponse {&nbsp; &nbsp; someProperty: string;&nbsp; &nbsp; someNumber: number;&nbsp; &nbsp; someArray: string[];}this._http.myMethod().subscribe((myMethodResponse: MyMethodResponse) => {&nbsp; &nbsp; // TypeScript now knows that these properties exists on the response object&nbsp; &nbsp; console.log(myMethodResponse.someArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; console.log(myMethodResponse.someNumber);&nbsp;&nbsp; &nbsp; console.log(myMethodResponse.someProperty);&nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript