首页 > 解决方案 > 在 component.ts 文件的 interface 文件夹下找不到我的 typescript 属性

问题描述

我的代码是这样的: -

我正在book.ts接口文件夹下创建一个(属性),我在这里创建了一个 book.ts 文件。但是我的 book.component.ts 没有找到我的 book.ts 属性。但是如果我使用一个接口(book.ts)在 book.component.ts 中。然后它工作正常。但我想将此界面(book.ts)保留在我的 component.ts 文件之外。我如何从我的组件访问它。

这是我的代码:-

应用程序/接口/book.ts

interface Book
{
    id: number;
    title: string;
    description: string;
    author: string;
    rate?: number;
    dateStart?: Date;
    dateRead? : Date;
}

应用程序/组件/books.component.ts

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



@Component({
  selector: 'app-books',
  templateUrl: './books.component.html',
  styleUrls: ['./books.component.css']
})


export class BooksComponent implements OnInit {

  
  
  public books: Book[];

  constructor() { }

  ngOnInit(): void {
  }

}

当我运行我的应用程序时,我发现了这个错误:-

在此处输入图像描述

文件夹结构:-

在此处输入图像描述

我如何解决这个问题。请帮忙。

更新

import { Component, OnInit } from '@angular/core';
import { Book } from '../interfaces/book'

@Component({
  selector: 'app-books',
  templateUrl: './books.component.html',
  styleUrls: ['./books.component.css']
})


export class BooksComponent implements OnInit {

  
  
  public books: Book[];

  constructor() { }

  ngOnInit(): void {
  }

}


export  interface Book
{
    id: number;
    title: string;
    description: string;
    author: string;
    rate?: number;
    dateStart?: Date;
    dateRead? : Date;
}

新错误:-

在此处输入图像描述

标签: angulartypescriptangular-materialangular11

解决方案


您没有Book在组件文件中导入。

加入export_book.ts

export interface Book {
  // ...
}

并在组件文件中导入

import { Book } from '../../interfaces/book'
// other imports and code 

推荐阅读