我知道 private void insertWeight() {
String weightString = edt1.getText().toString().trim();
if (weightString.isEmpty()) {
Log.v("MainActivity", "Test: Weight is EMPTY!!!");
finish();
}
if (weightString != null && !weightString.trim().isEmpty()) {
int i = Integer.parseInt(weightString);
Log.v("MainActivity", "Test: Weight is- " + i);
}
}
中的@Input
和@Output
将数据分别从“父对子”和“子对子”传递。在构建一个简单的应用程序时就达到了目标。
但是现在我已经有了真实的应用程序和路由,并且由于某种原因,我需要将数据从父组件传递到子组件,反之亦然。
app-routes.config.ts
Angular 7 (2+)
现在我需要将一些数据从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
app.component.html
ListJobsComponent
app-list-jobs.component.html
<div class="container">
<div class="page-header">
<h1>Hello Angular7 </h1>
</div>
<app-header></app-header>
<router-outlet></router-outlet>
</div>
app-list-jobs.component.ts
<div class="row">
{{parentComponentData}}
</div>
我知道它可以通过黑客方式实现,例如存储在localStorage或sessionStorage等中。
如何以正确的方式传递这些数据?
谢谢!