在Angular2 typescript类中找不到模块

时间:2018-05-08 12:41:49

标签: angular typescript

我有一个带有以下内容的打字稿文件:

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);
    }
}

当我编译时,它抱怨它不知道Book.cs文件在哪里。

这是错误:

  

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

然而,当我构建这条路时,我通过视觉工作室的智慧构建它。所以,该死的很清楚它在哪里。

这是我的项目结构:

enter image description here

有人能告诉我这里做错了什么吗?

2 个答案:

答案 0 :(得分:2)

似乎文件本身存在,但它有.cs扩展名,这意味着它是一个C#文件。在指定目录中搜​​索时,TypeScript编译器仅查找.ts.d.ts文件。

您应该重命名文件Book.ts并使用TypeScript来定义(根据我的推断)模型

答案 1 :(得分:1)

来自评论

  

这是vehicle.cs类:github.com/mosh-hamedani/vega/blob/master/Core/Models / ...我明白你在说什么,但我只是想知道为什么作者在ts代码中使用cs文件知道它不会工作。你看到他用的地方了吗?检查一下:github.com/mosh-hamedani/vega/blob / ...

您正在查看2个不同的文件。 /ClientApp/app/models/vehicle.ts/Core/Models/Vehicle.cs。您可以查看评论中发布的链接,以便亲自查看。这就是为什么这个应用程序转换/工作,因为它指向.ts文件而不是.cs文件恰好具有相同的名称(但不是扩展名或位置)。

将这个问题归咎于当你长时间看某事时所犯的错误之一,它发生在我们最好的人身上。

相关问题