简体   繁体   English

我在我的 angular-cli 项目“找不到模块”中收到此错误

[英]I get this error in my angular-cli project 'Cannot find module'

I am following a little Angular-cli tutorial, and in one of my component TS file, I import a class from another folder, but apparently it cannot be reached.我正在学习一个 Angular-cli 教程,在我的一个组件 TS 文件中,我从另一个文件夹导入了一个类,但显然无法访问它。

Here is my component file (display-user-data-form.component.ts):这是我的组件文件(display-user-data-form.component.ts):

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

@Component({
  selector: 'display-user-data-form',
  templateUrl: './display-user-data-form.component.html',
  styleUrls: ['./display-user-data-form.component.css']
})
export class DisplayUserDataFormComponent implements OnInit {
    
    user: UserInfoModel = new UserInfoModel({guid: "D21ds12x", 
        customerUid: "cust2dsa12dsa", 
        first_name: "John", 
        last_name: "Doe", 
        email: "email@email.com", 
        zipcode: 10283,
        password: "Idasn2x2#"});

  constructor() { }

  ngOnInit(): void {
  }

}

You see I am importing a class called "UserInfoModel" from a folder named "models".您会看到我正在从名为“models”的文件夹中导入一个名为“UserInfoModel”的类。

My file tree looks like this in the 'app' folder:我的文件树在“app”文件夹中如下所示:

app
-display-user-data-form
--display-user-data-form.component.ts
--display-user-data-form.component.html
--display-user-data-form.component.css
-models
--UserInfoModel.ts

I really don't know why it can't find the module and can't figure out why the path I give is not correct.我真的不知道为什么它找不到模块,也无法弄清楚为什么我给出的路径不正确。

Thanks in advance for your answers!提前感谢您的回答!

You need to specify the file names too.您还需要指定文件名。 As the UserInfoModel in import { UserInfoModel } is actually the class name, so you need to specify which file you will get your class from, so it will be like this:由于UserInfoModelimport { UserInfoModel }实际上是类的名字,所以你需要指定是哪个文件,你会得到你的类从,所以这将是这样的:

import { UserInfoModel } from '../models/UserInfoModel';

The first UserInfoModel is the class name and the second one is the file name第一个UserInfoModel是类名,第二个是文件名

However, there is an option to not include the file name using the Barrel Import.但是,有一个选项可以使用 Barrel Import 不包含文件名。 You can read more about it here你可以在这里阅读更多关于它的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM