angular - 我无法使用 Angular 中的输出和 EventEmitter 在应用程序组件中获取组件(帖子)的属性
问题描述
它说任何类型的参数是不可分配的。
我应该在 app.component 中导入其他内容吗?
你能告诉我我做错了什么吗?app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'test';
posts=[];
onAddedPost(post){
this.posts.push(post)
}
}
这就是问题所在。onAddedPost 中的帖子是问题所在。创建后.component.ts
import { sharedStylesheetJitUrl } from '@angular/compiler';
import { Component, EventEmitter, Injectable, Output, OutputDecorator } from '@angular/core';
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html',
styleUrls: ['./post-create.component.css']
})
export class PostCreateComponent {
enteredContent='';
enteredTitle='';
@Output() postCreated = new EventEmitter();
onAddPost(){
const post={title:this.enteredTitle,content:this.enteredContent};
this.postCreated.emit(post);
}
}
app.component.html
<app-header></app-header>
<main>
<app-post-create (postCreated)="onPostAdded($event)" ></app-post-create>
<app-post-list [posts]="storedPosts"></app-post-list>
</main>
app.module.ts
@NgModule({
declarations: [
AppComponent,
PostCreateComponent,
HeaderComponent,
PostListComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
BrowserAnimationsModule,
MatInputModule,
MatCardModule,
MatButtonModule,
MatToolbarModule,
MatExpansionModule,
],
bootstrap: [AppComponent]
})
export class AppModule { }
解决方案
很可能这是一个 TSLint 错误,因为它无法推断类型。注意:这不会影响应用程序,因为编译后的 JS 无法识别 TS 类型。
但是要解决它,您可以定义该类型的 Typescript 接口。
- 定义模型
post.ts
export interface Post {
title?: string;
content?: string;
}
- 使用此模型在两个组件中定义类型。
创建后.component.ts
import { sharedStylesheetJitUrl } from '@angular/compiler';
import { Component, EventEmitter, Injectable, Output, OutputDecorator } from '@angular/core';
import { Post } from './post';
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html',
styleUrls: ['./post-create.component.css']
})
export class PostCreateComponent {
enteredContent='';
enteredTitle='';
@Output() postCreated: EventEmitter<Post> = new EventEmitter<Post>();
onAddPost(){
const post: Post = {
title: this.enteredTitle,
content: this.enteredContent
};
this.postCreated.emit(post);
}
}
app.component.ts
import { Component } from '@angular/core';
import { Post } from './post';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'test';
posts: Post[] = [];
onAddedPost(post: Post) {
this.posts.push(post);
}
}
推荐阅读
- database - 无法在 Azure Devops 构建管道中查看数据库差异报告
- apache - 如何在 apache 2.4 上将 403 页面伪装成 404?
- selenium - 在 selenium 中有一个测试用例我想用不同版本的 chrome 浏览器测试这个测试用例怎么做?
- java - 在纱线集群模式下访问 rdd 操作中的数组变量时出现问题
- java - 如何使用 Apache Sentry Java Client API & 是否有用户指南文档?
- firebase - 为什么 firebase 使用 Firebase Storage 中的相同图像填充我的视图
- javascript - 根据状态更改 JSON 的 HTML 表格的单元格颜色
- qt - 我应该使用哪种方法来获取 Android Build Version SDK_INT?
- bootstrap-4 - 下拉菜单文本对齐,css工作但引导失败?
- ios - 无法在 iPhone XS 和 iPhone XS Max 上缩放资产以保持一致性