angular - 在 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;
}
新错误:-
解决方案
您没有Book
在组件文件中导入。
加入export
_book.ts
export interface Book {
// ...
}
并在组件文件中导入
import { Book } from '../../interfaces/book'
// other imports and code
推荐阅读
- azure-ad-b2c - 经过一定时间后的 MFA 提示
- javascript - 在 iframe 上带有 html2canvas 的颜色选择器滴管反应
- python - 使用气流模块的 PythonVirtualenvOperator 无法使用 AttributeError 执行:模块 'airflow' 没有属性 'utils'
- javascript - 如何防止包含链接的 td 在 Outlook 电子邮件的表格中被点击?
- disk - 克隆过程中磁盘会损坏吗?
- javascript - JQuery有条件地设置嵌套输入元素的格式化文本
- wordpress - 类别最后一页上的文字(Woocommerce)
- firebase - Firebase API - 从上传的图片中获取 gs url
- pagespeed-insights - 与 Pagespeed Insights 不一致
- python - TypeError: 'int' 和 'str' 的实例之间不支持'<'。标记化字符串 100% 整数