angular - 在Angular中进行路由时如何将数据从组件传递到组件
问题描述
我知道@Input
& @Output
inAngular 7 (2+)
分别将数据从 Parent 传递给 Child 和 Child 传递给 Parent 。它在构建一个简单的应用程序时达到了目标。
但是现在我有了真实的应用程序和路由,出于某种原因,我需要将数据从父组件传递到子组件,反之亦然。
应用程序路由.config.ts
import { NgModule } from '@angular/core';
/* all imports */
const routes: Routes = [
{ path: '', pathMatch: 'full', component: AppComponent },
{ path: 'jobs', canActivateChild: [AuthGuard], component: HomeComponent,
children: [
{ path: 'list', component: ListJobsComponent },
{ path: 'create', component: CreateJobComponent },
]},
{ path: 'not-found', component: NotFoundComponent },
{ path: '**', redirectTo: 'not-found' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule]
})
export class AppRoutingModule { }
现在我需要将一些数据传递AppComponent
给ListJobsComponent
app.component.html
<div class="container">
<div class="page-header">
<h1>Hello Angular7 </h1>
</div>
<app-header></app-header>
<router-outlet></router-outlet>
</div>
应用程序列表jobs.component.html
<div class="row">
{{parentComponentData}}
</div>
应用程序列表jobs.component.ts
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input('parentComponentData') parentComponentData: string;
@Output() onHit: EventEmitter<string> = new EventEmitter<string>();
constructor() { }
ngOnInit() {
}}
我知道它可以以黑客方式实现,例如存储在 localStorage 或 sessionStorage 等。
如何以正确的方式传递这些数据?
谢谢!!
解决方案
推荐阅读
- python - 将事件添加到正在运行的 sched.scheduler
- c# - 使用 GCHandle.Alloc() 访问和操作固定内存的正确方法是什么?
- scikit-learn - 将决策树设为弱学习器,但得分 > 80%
- java - firebase.auth.FirebaseUser.linkWithCredential 抛出空指针异常
- javascript - 读取本地 JSON 文件并将其中每个对象的确定键返回到数组中
- java - 检查方法的返回是否为 List
有反射 - java - 为什么我不断收到错误:“错误:(20、23)java:不兼容的类型:java.lang.String 无法转换为 Lab1.Person”
- spring-security - OAuth2FeignRequestInterceptor 的替代方案,依赖于已弃用的类
- lua - lua中的Python itertools产品功能
- gcc - 如何使用 GCC 编译指示优化选项?(例如 max-peeled-insns)