扩展HttpErrorResponse导致错误找不到模块'@ angular/common/http' [英] Extending HttpErrorResponse leads to error Cannot find module '@angular/common/http'

查看:129
本文介绍了扩展HttpErrorResponse导致错误找不到模块'@ angular/common/http'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型(在单独的文件中定义),该模型使用自定义属性扩展了HttpErrorResponse.自定义属性是具有少量属性的界面:

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

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

export class ApiErrorResponse extends HttpErrorResponse {
  public error: ApiErrorBody;
}

此代码在调试模式下可以正常工作,但是在编译为prod时,我在运行时看到错误:

错误:找不到模块'@ angular/common/http'

如果我删除扩展名并仅填充与HttpErrorResponse相同的属性,则代码在prod中可以正常工作,但是我需要保留扩展名语法.

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

// no extension
export class ApiErrorResponse {
  public error: ApiErrorBody;
  public status: number;
  public message: string;
  piblic url: string;
}

此模型由多个组件和拦截器引用,并且所有组件都依赖于@ angular/common/http,所以我的问题是-我在这里还错过了什么,以及为什么它可以在调试中工作但在其中不起作用生产模式?

解决方案

问题出在package.json
包@ angular/common在devDependencies中.根据Electron builder的文档( https://www.electron.build/configuration/contents )开发依赖项从未复制到生成的程序包中.

将以"@angular"开头的依赖项移动到package.json的devDependencies部分中的依赖项已解决.

I have a model (defined in separate file) which extends HttpErrorResponse with custom property. Custom property is an interface with few properties:

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

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

export class ApiErrorResponse extends HttpErrorResponse {
  public error: ApiErrorBody;
}

This code works fine in debug mode, but when compiling to prod, I see error at runtime:

Error: Cannot find module '@angular/common/http'

If I remove extension and just populate the same properties as HttpErrorResponse has, code works fine in prod, but I need to keep extension syntax.

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

// no extension
export class ApiErrorResponse {
  public error: ApiErrorBody;
  public status: number;
  public message: string;
  piblic url: string;
}

This model is referenced from multiple components and interceptors and all of them are provided with dependency on @angular/common/http, so my questions are - what else I missed here and why it works in debug but doesn't work in prod mode?

解决方案

The problem was in package.json
Package @angular/common was in devDependencies. According to documentation of Electron builder (https://www.electron.build/configuration/contents) dev dependencies never copied to resulting package.

Moving dependencies that start with "@angular" to dependencies from devDependencies section in package.json solved the issues.

这篇关于扩展HttpErrorResponse导致错误找不到模块'@ angular/common/http'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆