Angular 角度:从父级向子级传递消息无效
我试图将一个值从父级传递给子级。 但是消息只显示在父组件中,而不显示在子组件中。 我的代码怎么了? 或者我需要在Angular 角度:从父级向子级传递消息无效,angular,Angular,我试图将一个值从父级传递给子级。 但是消息只显示在父组件中,而不显示在子组件中。 我的代码怎么了? 或者我需要在应用程序模块中导入某些内容吗 家长: @Component({ selector: 'app-neues-spiel', template: ` <app-seating-order [childMessage]="parentMessage"></app-seating-order> `, styleUrls: ['./neues-
应用程序模块中导入某些内容吗
家长:
@Component({
selector: 'app-neues-spiel',
template: `
<app-seating-order [childMessage]="parentMessage"></app-seating-order>
`,
styleUrls: ['./neues-spiel.component.scss']
})
export class NeuesSpielComponent implements OnInit {
parentMessage = "message from parent";
编辑:
控制台或网页包中没有错误
但如果我结束发球,
控制台显示错误,但不确定是否相关:
没有html,有component.html文件和代码,但我对其进行了注释,用模板对其进行了测试
编辑:
应用程序模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AngularFireModule} from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { FormsModule } from '@angular/forms';
import { AuthService } from './core/auth.service';
import { AngularFireDatabase } from 'angularfire2/database-deprecated';
import { environment } from './../environments/environment';
import { AppComponent } from './app.component';
import { AppNavbarComponent } from './app-navbar/app-navbar.component';
import { AngularFontAwesomeModule } from 'angular-font-awesome';
import { HomeComponent } from './home/home.component';
import { SettingsComponent } from './settings/settings.component';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { RouterModule, Routes, CanActivate } from '@angular/router';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { NeuesSpielComponent } from './neues-spiel/neues-spiel.component';
import { SeatingOrderComponent } from './seating-order/seating-order.component';
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'settings', component: SettingsComponent },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'neues-spiel', component: NeuesSpielComponent },
{ path: 'seating-order', component: SeatingOrderComponent },
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
declarations: [
AppComponent,
AppNavbarComponent,
HomeComponent,
SettingsComponent,
LoginComponent,
RegisterComponent,
PageNotFoundComponent,
NeuesSpielComponent,
SeatingOrderComponent
],
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
AngularFireAuthModule,
AngularFontAwesomeModule,
RouterModule.forRoot(appRoutes),
FormsModule,
],
providers: [AuthService, AngularFireDatabase],
bootstrap: [AppComponent]
})
export class AppModule { }
我不知道为什么这不起作用
但是,您可以更改此项并检查其是否有效吗?在您的子组件中,不使用@Input,您可以试试这个吗
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-seating-order',
inputs: ['childMessage'],
template: `
<div>{{childMessage}}</div>
`,
styleUrls: ['./seating-order.component.scss']
})
export class SeatingOrderComponent implements OnInit {
constructor() { }
}
从'@angular/core'导入{Component,OnInit,Input};
@组成部分({
选择器:“应用程序座位顺序”,
输入:['childMessage'],
模板:`
{{childMessage}}
`,
styleUrls:['./座位顺序.component.scss']
})
导出类SeatingOrderComponent实现OnInit{
构造函数(){}
}
您应该更改子元素的模板,将其放置在某个元素中。例如:
template: `
<div><h1>{{childMessage}}</h1></div>
`,
模板:`
{{childMessage}}
`,
控制台或网页包中没有错误
但如果我结束发球,
控制台显示错误,但不确定是否相关:
没有html,有带代码的component.html文件,但我对其进行了注释以使用模板进行测试。这会引发任何异常/错误吗?我觉得不错。你能添加结果页面的HTML吗?你的浏览器控制台上有任何错误吗?@parvanesh检查下面的演示,检查过了,但它不起作用。关于我关机时遇到的错误,请参阅下面的帖子。刚重新启动我的电脑并再次启动ng,出现了一个错误“No trace…”,但它消失得太快,没有再次出现。哦,是的,可能是这样。别介意,我也根据这个编辑了我的答案:)
template: `
<div><h1>{{childMessage}}</h1></div>
`,