【问题标题】:'post' only refers to a type, but is being used as a value here'post' 仅指一种类型,但在这里用作值
【发布时间】:2021-07-22 15:19:21
【问题描述】:

我在 push(post) 时在这个 component.ts 文件中遇到错误。 错误是 'post' 仅指一种类型,但在此处用作值。

import { Component } from '@angular/core';
import { post } from './comp/list/list.module';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
spost:post[]=[];

onpostadd(){
  this.spost.push(post);
}
}

这是model.ts文件

export interface post {

  title: string;
  content : string;

}

【问题讨论】:

  • 嗯,它是一种类型。而您将其用作价值。所以错误信息是正确的。我不知道你想要达到什么目的。
  • 因为post 是一种类型。你想推动什么价值?
  • 没看懂能不能改正

标签: javascript angular typescript


【解决方案1】:

正如它所说:您只是在导出一个接口(它只是一个类型)post,而不是一个实际值。因此,您不能将其用作值。

您的onpostadd() 可能期望post 作为您未添加的参数。

此外,CamelCase 类型名称也很常见,类似于类,例如请改用Post。这使得它很难与小写变量名混淆。

【讨论】:

  • 没看懂能不能改正
  • 我不太了解您的代码或意图,不知道如何修复它。您的 onpostadd 正在尝试将 post 推送到您的数组,但 post 是您导入的类型,而不是来自任何地方的实际帖子。我假设您打算写onpostadd(post) { ... }
猜你喜欢
  • 2018-01-14
  • 2019-10-08
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2018-12-30
  • 2019-09-16
  • 2017-08-16
相关资源
最近更新 更多