为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

婷婷同学_ 2022-01-13 17:04:00
我整天都在工作。一整天一切正常。重新启动服务器(ng serve)。现在突然有很多错误。我设法解决了大部分问题,但我被这个问题困住了。这是组件 .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);  }}它工作了一整天,突然出现这些奇怪的错误。有谁知道可能出了什么问题?非常感谢!
查看完整描述

2 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

从您的 http 调用中删除对象。使用泛型是可选的,尤其是在您没有输入回复的情况下。


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('https://localhost:44398/api/boggle');

  }


  myWordMethod(word) {

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

    return this.http.get(url);

  }

}

在 brews 上,将其声明为 any:


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: any;


  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);

    });

  }


查看完整回答
反对 回复 2022-01-13
?
暮色呼如

TA贡献1853条经验 获得超9个赞

您可以通过简单地将它们定义为 any 类型来忽略它们。例如;


myWordMethod(word: any) {

    ..

}


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

    ..

});

也就是说,通常首选为 TypeScript 声明实际类型。例如,如果您的 API 发回具有特定属性的通用对象,则将其声明为这样;


interface MyMethodResponse {

    someProperty: string;

    someNumber: number;

    someArray: string[];

}


this._http.myMethod().subscribe((myMethodResponse: MyMethodResponse) => {

    // TypeScript now knows that these properties exists on the response object

    console.log(myMethodResponse.someArray);           

    console.log(myMethodResponse.someNumber); 

    console.log(myMethodResponse.someProperty); 

});


查看完整回答
反对 回复 2022-01-13
  • 2 回答
  • 0 关注
  • 1017 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信