简体   繁体   中英

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.

Here's the error:

(TS) Cannot find Module '../../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. The TypeScript compiler looks only for .ts and .d.ts files when searching in the specified directories.

You should rename the file Book.ts and use TypeScript to define (from what I infer) the model

From the comments

Here is the vehicle.cs class: 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. Did you see where he used it? Check this: github.com/mosh-hamedani/vega/blob/…

You are looking at 2 different files. /ClientApp/app/models/vehicle.ts and /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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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