简体   繁体   English

在Angular2打字稿类中找不到模块

[英]Cannot find module in Angular2 typescript class

I have a typescript file with the following content: 我有一个包含以下内容的打字稿文件:

import { Component, OnInit } from '@angular/core';
import { BookService } from "../../services/book.service";
import { Book } from "../../Models/book";


@Component({
    selector: 'app-book-list',
    templateUrl: './book-list.component.html',
    styleUrls: ['./book-list.component.css']
})
export class BookListComponent implements OnInit {


    books: Book[];

    constructor(private bookService: BookService) {

    }

    ngOnInit() {
        this.bookService.getBooks()
            .subscribe(books => this.books = books);
    }
}

When I compile, it is complaining that it doesn't know where the Book.cs file is. 当我编译时,它抱怨说它不知道Book.cs文件在哪里。

Here's the error: 这是错误:

(TS) Cannot find Module '../../Models/Book' (TS)找不到模块'../../Models/Book'

However, when I was constructing this path, I constructed it through the visual studio intellisence. 但是,当我构建这条路径时,我是通过视觉工作室的智慧来构建它的。 So, it damn well knows where it is. 因此,该死的人都知道它在哪里。

Here's my project structure: 这是我的项目结构:

在此处输入图片说明

Can someone tell me what am I doing wrong here? 有人可以告诉我我在做什么错吗?

It seems like the file itself exists, but it has a .cs extension, meaning that it's a C# file. 似乎文件本身存在,但文件扩展名为.cs ,表示它是C#文件。 The TypeScript compiler looks only for .ts and .d.ts files when searching in the specified directories. 在指定目录中搜​​索时,TypeScript编译器仅查找.ts.d.ts文件。

You should rename the file Book.ts and use TypeScript to define (from what I infer) the model 您应该重命名文件Book.ts并使用TypeScript定义(根据我的推断)模型

From the comments 从评论

Here is the vehicle.cs class: github.com/mosh-hamedani/vega/blob/master/Core/Models/…. 这是vehicle.cs类:github.com/mosh-hamedani/vega/blob/master/Core/Models/…。 I do understand what you are saying, but I just want to know why the author used the cs file in the ts code knowing well tht it wont work. 我的确知道您在说什么,但我只是想知道为什么作者在ts代码中使用了cs文件,但知道这行不通。 Did you see where he used it? 你看到他在哪里用过的吗? Check this: github.com/mosh-hamedani/vega/blob/… 检查一下:github.com/mosh-hamedani/vega/blob/…

You are looking at 2 different files. 您正在查看2个不同的文件。 /ClientApp/app/models/vehicle.ts and /Core/Models/Vehicle.cs . /ClientApp/app/models/vehicle.ts/Core/Models/Vehicle.cs You can check the links you posted in your comment to see for yourself. 您可以查看自己在评论中发布的链接,以自己查看。 That is why this application transpiles/works, because it is pointing to a .ts file and not a .cs file which happens to have the same name (but not extension or location for that matter). 这就是为什么该应用程序进行编译/工作的原因,因为它指向的是.ts文件,而不是恰好具有相同名称(但不是扩展名或位置)的.cs文件。

Chalk this one up to one of those mistakes that you make when you have been looking at something for too long, it happens to the best of us. 将此问题归因于您长时间查看某件事时所犯的错误之一,这对我们所有人来说都是偶然的。

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

相关问题 找不到模块Angular2 Typescript - Cannot find the module Angular2 Typescript Typescript Angular2错误找不到模块angular / x - Typescript Angular2 error cannot find module angular/x 找不到外部模块'angular2 / angular2' - Angular2 w / Typescript - Cannot find external module 'angular2/angular2' - Angular2 w/ Typescript Angular2 代码中的 TypeScript 错误:找不到名称“模块” - TypeScript error in Angular2 code: Cannot find name 'module' 找不到模块'angular2 / angular2' - Cannot find module 'angular2/angular2' IntelliJ和Angular2 / TypeScript-错误TS2307:找不到模块“ angular2 /…” - IntelliJ & Angular2 / TypeScript - error TS2307: Cannot find module 'angular2/…' 在VS2013和打字稿1.6.2中找不到模块'angular2 / angular2' - Cannot find module 'angular2/angular2' in VS2013 and typescript 1.6.2 使用gulp-typescript获得“无法找到模块'angular2 / angular2'” - Got “Cannot find module 'angular2/angular2'” using gulp-typescript TS2307:在 TypeScript 上导入 Angular2 时找不到模块“angular2/core” - TS2307: Cannot find module 'angular2/core' while importing Angular2 on TypeScript Angular2 TypeScript - 错误TS2307:找不到模块'angular2 / core' - Angular2 TypeScript - error TS2307: Cannot find module 'angular2/core'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM