NativeScript 应用程序的“Error-ReferenceError-ErrorEvent 未定义”

2023-12-20

我是 NativeScript 的新学习者。我正在按照 Coursera 上的视频教程使用 NativeScript 创建 Android 项目,并且我收到了由这部分代码引起的上述错误:

process-httpmsg.service.ts :

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

import { throwError } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ProcessHTTPMsgService {

  constructor() { }

  public handleError(error: HttpErrorResponse | any) {
    let errMsg: string;

    if (error.error instanceof ErrorEvent) {
      errMsg = error.error.message;
    } else {
      errMsg = `${error.status} - ${error.statusText || ''} ${error.message}`;
    }

    return throwError(errMsg);
  }
}

dish.services.ts:

import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';

import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';

@Injectable({
  providedIn: 'root'
})
export class DishService {

  constructor(private http: HttpClient,
    private processHTTPMsgService: ProcessHTTPMsgService) { }

  getDishes(): Observable<Dish[]> {
    return this.http.get<Dish[]>(baseURL + 'dishes')
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getDish(id: string): Observable<Dish> {
    return this.http.get<Dish>(baseURL + 'dishes/' + id)
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getFeaturedDish(): Observable<Dish> {
    return this.http.get<Dish[]>(baseURL + 'dishes?featured=true').pipe(map(dishes => dishes[0]))
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }
}

menu.component.html:

<ActionBar title="Menu" class="action-bar">
</ActionBar>
<StackLayout class="page">
    <ListView [items]="dishes" class="list-group" *ngIf="dishes">
        <ng-template let-dish="item">
            <StackLayout orientation="horizontal" class="list-group-item">
                <Image row="0" col="0" rowSpan="2" height="108" width="108"  [src]="BaseURL + dish.image" class="thumb p-16"></Image>
                <GridLayout class="list-group-item" rows="auto *" columns="*">
                    <Label row="0" col="0" [text]="dish.name" class="list-group-item-heading"></Label>
                    <Label row="1" col="0" class="list-group-item-text" [text]="dish.description"></Label>   
                </GridLayout>
            </StackLayout>
        </ng-template>
    </ListView>
    <ActivityIndicator busy="true"  *ngIf="!(dishes || errMess)" width="50" height="50" class="activity-indicator"></ActivityIndicator>
    <Label *ngIf="errMess" [text]="'Error: ' + errMess"></Label>
</StackLayout>

menu.component.ts:

import { Component, OnInit, Inject } from '@angular/core';
import { Dish } from '../shared/dish';
import { DishService } from '../services/dish.service';


@Component({
  selector: 'app-menu',
  moduleId: module.id,
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {

  dishes: Dish[];
  errMess: string;

  constructor(private dishService: DishService,
    @Inject('baseURL') private baseURL) { }

  ngOnInit() {
    this.dishService.getDishes()
      .subscribe(dishes => this.dishes = dishes,
        errmess => this.errMess = <any>errmess);
  }

}

我不知道需要放置项目代码的哪些其他部分,所以请让我知道。

EDIT: baseURL.ts:

// export const baseURL = "http://localhost:3000/";

export const baseURL = "http://192.168.1.5:3000/";

正如我已经提到的,你不能这样做error.error instanceof ErrorEvent as ErrorEvent是接口而不是类。你可以做error.error instanceof HttpErrorResponse.

你不能打http://localhost:3000从你的模拟器/仿真器中,你应该通过你的机器的 IP 来访问你的 api,因为它们位于同一网络中,但技术上不同(模拟)设备。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NativeScript 应用程序的“Error-ReferenceError-ErrorEvent 未定义” 的相关文章

  • 错误:尝试扩展 tslint-microsoft-contrib 时找不到模块“tslint/lib/lint”

    这就是我们的环境 gt node modules bin tslint version 4 0 1 gt npm install save dev tslint microsoft contrib email protected cdn c
  • Typescript 递归地更改属性类型

    如何递归地改变属性类型 我只能在单一维度上做到这一点 从实用程序类型 https github com piotrwitek utility types似乎有可能实现这一目标 我已经尝试过多次但无法做到正确 type Convert
  • TypeScript 3 参数列表交集类型

    打字稿 3 https blogs msdn microsoft com typescript 2018 07 30 announcing typescript 3 0 tuples and parameters改进的参数列表 我正在查看类
  • 具有相同父布局角度的功能模块路由

    我想使用相同的布局 在应用程序模块 ts 对于不同的功能模块 每个模块都有自己的路由 以及一个单独的登录 注册布局 没有侧面菜单 页眉和页脚 到目前为止我尝试过这个 app app component html
  • angular2存储许多字符串值

    我有一些必须在许多组件中使用的字符串常量值 我不想在各个组件中硬编码或将它们写为字符串 在 Angular2 应用程序中维护它们的最佳位置在哪里 解决方案之一是将字符串放置在共享服务中 将服务注入到需要该属性的每个组件中 如果您的字符串不是
  • 在 Typescript 中创建动态变量引用

    提前感谢任何提供帮助的人 对于有经验的人来说 这似乎是一个简单的答案 但我已经浏览了互联网和几本参考书 并没有找到这个问题的直接答案 所以希望它也对其他人有帮助 我目前正在从 Actionscript 过渡到 Typescript 并且对普
  • Typescript 中的子类化

    我在 Typescript 中有一个存储类 它实现了Storage界面 说MyStorage 但它的方法太多了 所以我想用它来代替MyStorage getCandy MyStorage Candies getCandies 该结构可能看起
  • Observable:如果前面有map(),为什么不会导致subscribe()成功?

    我参考这个post https stackoverflow com questions 34671715 angular2 http get map subscribe and observable pattern basic unders
  • 复选框警报控制器 - 添加 3 个按钮

    我用 IONIC 2 Beta 版本制作了一个应用程序 我正在使用复选框警报控制器 并添加两个按钮 确定 和 取消 现在我需要在警报控制器中再添加一个按钮 我在下面实现了添加一个按钮 alert addButton Cancel alert
  • 使用 Object.entries() 时保留键类型

    这是我的问题的一个例子 ts 游乐场演示 https www typescriptlang org play code C4TwDgpgBAKg1hEUC8UDkAzA9ltUA 6ARgIYBOehaAJhBiQK4A2wA0omgFCc
  • “未知”与“任何”

    TypeScript 3 0 引入unknown根据他们的 wiki 输入 未知现在是保留类型名称 因为它现在是内置类型 根据您对未知的预期用途 您可能需要删除 完全声明 有利于新引入的未知类型 或者 将其重命名为其他名称 有什么区别unk
  • ionic2 对菜单项应用 ngx-translate

    我正在使用 ngx translate 来支持多语言 并且运行良好 但我也想申请菜单项 我该如何实现这一目标 我有 3 个菜单项 我想更改每个标题的语言 ts file appPages PageObj title Profile comp
  • 打字稿 - 找不到导出

    我在模块 A 中有这个 Typescript 文件 somefile tsx import remAuto from tidee life theme 在模块 B 中 我导出了 index js 文件remAuto index js imp
  • 在从命令行运行的 NestJS 脚本中使用服务

    我知道如何从命令行运行脚本 使用npm or npx ts node script ts 正如所述here https stackoverflow com questions 60704316 run nestjs script from
  • Angular2:setTimeout仅调用一次

    我正在 Angular2 中实现需要使用的功能setTimeout My code public ngAfterViewInit void this authenticate loop private authenticate loop s
  • 如何将 zingchart 实现到 Angular2 中

    我有一个现有的项目 我想在其上实施 zingcharts 我尝试了 3 个不同的教程 主要来自 https blog zingchart com 2016 07 19 zingchart and angular 2 charts back
  • TypeScript 中泛型的不安全隐式转换

    TypeScript 编译器tsc编译以下代码 即使使用 strict旗帜 然而 该代码包含一个基本错误 而在 Java 或 C 等语言中可以避免这种错误 interface IBox
  • 在 Typescript 中隐式创建一个元组

    有没有一种方法无需类型提示即可在 Typescript 中创建元组 如果我只是这样做 const tuple 1 2 元组的类型number 我能得到的最接近的单线是 const tuple number number 1 2 我错过了什么
  • 角度 2 通道数组到自定义验证器(模板驱动形式)

    我需要将一个对象数组传递给 a2 自定义验证器 然后我想根据该数组中的记录验证模板驱动表单字段的值 但是我无法检索验证器内的对象 我唯一能看到的是它的名称作为字符串 如有任何帮助 我们将不胜感激
  • CSF3 中的 Typescript 支持

    我正在使用 CSF3 编写故事 并且我想正确注释我的故事 我用谷歌搜索过 基本上找不到答案 我尝试过一些技巧here https github com storybookjs storybook issues 7677但没有任何效果 我发现

随机推荐