mcfred
mcfred

Reputation: 1391

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:

enter image description here

Can someone tell me what am I doing wrong here?

Upvotes: 0

Views: 113

Answers (2)

Igor
Igor

Reputation: 62213

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.

Upvotes: 1

Hristo Georgiev
Hristo Georgiev

Reputation: 2519

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

Upvotes: 2

Related Questions