You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular同目录下导入模型报错求助

同一文件夹下导入Angular Drink模型报错求助

我是Angular新手,在同一文件夹下将Drink模型导入其他文件时出现错误,相关代码如下:

drink.ts

export class Drink {
  idDrink!: string;
  strDrink!: string;
  strCategory!: string;
  strAlcoholic!: string;
  strGlass!: string;
  strInstructionsES!: string;
  strDrinkThumb!: string;
  strIngredient1!: string;
  strIngredient2!: string;
  strIngredient3!: string;
  strIngredient4!: string;
  strIngredient5!: string;
  strIngredient6!: string;
}

response.ts

import { Drink } from './drink';

export interface Response {
  data: Drink[];
}

drinkResponse.ts

import { Drink } from './drink';

export class DrinkResponse {
  data!: Drink[];
}

apiResponse.ts

import { Drink } from './drink';
import { DrinkResponse } from './drinkResponse';

export class ApiResponse implements DrinkResponse {
  data: Drink[];

  constructor() {
    this.data = [{
        idDrink:"string",
        strDrink:"string",
        strCategory:"string",
        strAlcoholic:"string", 
        strGlass:"string",
        strInstructionsES:"string",
        strDrinkThumb:"string",
        strIngredient1:"string",
        strIngredient2:"string",
        strIngredient3: "string",
        strIngredient4:"string",
        strIngredient5:"string",
        strIngredient6:"string"
    }];
  }
}

我已经尝试了所有能想到的方法,但仍未找到错误所在,报错信息显示找不到模块 './drink' 或对应的类型声明。希望能得到帮助。

常见排查与解决方向

  • 核对文件名大小写:如果你的系统是Linux/macOS,文件名区分大小写,确认Drink类的实际文件名(比如是Drink.ts还是drink.ts)和导入语句中的路径完全一致。
  • 检查路径拼写:确保导入路径./drink拼写无误,没有多余或缺失的字符,同一文件夹下的相对路径格式正确。
  • 验证TypeScript配置:打开tsconfig.json,确认include数组包含当前文件夹(比如"src/app/your-folder/**/*"),确保所有相关.ts文件都被纳入编译范围。
  • 清理项目缓存:运行ng clean清除Angular缓存,再执行ng serve重启项目,避免缓存导致的异常。
  • 尝试添加文件扩展名:部分场景下需要明确导入.ts扩展名,比如改为import { Drink } from './drink.ts';后测试。

内容的提问来源于stack exchange,提问作者Benjamin Wellmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:22:21