在 Angular2 中的两个组件之间传递数据会产生绑定错误
Passing Data Between Two Components In Angular2 Produces Binding Error
我一直在寻找 SO 来解决我的问题,我知道它被问了很多次,但它对我的问题没有用。
我有两个组件:
部门
全布局
整个问题是尝试使用 @Inputs()
.
将数据从全布局组件传递到部门组件
下面我将先向您展示我的文件夹结构,然后是我的代码。
结构
Department Folder Structure
Layout-Folder Structure
departments.component.ts
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'app-departments',
templateUrl: './departments.component.html',
inputs: ['valueToPass']
})
export class DepartmentsComponent implements OnInit {
@Input() valueToPass;
public _departments;
constructor () { }
ngOnInit() {
console.log(this.valueToPass.nam);
}
}
departments.module.ts
// import{...}...
// assume necessary imports above
@NgModule({
imports: [
DepartmentsRoutingModule,
CommonModule,
MaterialModule.forRoot()
],
declarations: [ DepartmentsComponent ]
})
export class DepartmentsModule {}
full-layout.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './full-layout.component.html',
})
export class FullLayoutComponent implements OnInit {
public disabled:boolean = false;
public status:{isopen:boolean} = {isopen: false};
constructor (){}
ngOnInit (): void {}
dropDownItems = [
{ routerLink: '/departments', name: 'Artshums' },
{ routerLink: '/components/social-buttons', name: 'Dentistry' },
{ routerLink: '/components/cards', name: 'Law' },
{ routerLink: '/components/forms', name: 'IOPPN' },
{ routerLink: '/components/modals', name: 'LSM' },
{ routerLink: '/departments', name: 'NMS' },
{ routerLink: '/components/tables', name: 'Nursing' },
{ routerLink: '/components/tabs', name: 'SSPP' },
{ routerLink: '/components/tabs', name: 'Health' }
];
onClick(_container: HTMLElement) {
//this.clickedElement = _container.innerText;
//console.log(this.clickedElement);
}
}
full-layout.component.html
<ul class="nav-dropdown-items">
<li class="nav-item" *ngFor="let item of dropDownItems">
<a #clicked class="nav-link" routerLinkActive="active" [routerLink]="[item.routerLink]" (click)="onClick(clicked)">
<i class="icon-puzzle"></i>{{item.name}}
<app-departments [valueToPass] = "item"></app-departments>
</a>
</li>
</ul>
app.module.ts
// import {...}...
// assume necessary imports
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
DropdownModule.forRoot(),
TabsModule.forRoot(),
ChartsModule,
MaterialModule.forRoot(),
HttpModule,
JsonpModule,
DepartmentsModule
],
declarations: [
AppComponent,
FullLayoutComponent,
SimpleLayoutComponent,
NAV_DROPDOWN_DIRECTIVES,
BreadcrumbsComponent,
SIDEBAR_TOGGLE_DIRECTIVES,
AsideToggleDirective
],
providers: [{
provide: LocationStrategy,
useClass: HashLocationStrategy
}],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的问题就出在这里。当我 运行 这个程序时,我遇到了绑定错误,我不确定为什么以及如何修复它。
无法绑定到 'valueToPass',因为它不是 'app-departments' 的已知 属性。
如果 'app-departments' 是一个 Angular 组件并且它有 'valueToPass' 输入,然后验证它是该模块的一部分。
如果 'app-departments' 是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到此组件的“@NgModule.schemas”以禁止显示此消息。
如果能提供小小的帮助,我们将不胜感激。
谢谢冠军!
当您使用自定义选择器时,就像您使用这个:<app-departments>
,有时您需要将 "CUSTOM_ELEMENTS_SCHEMA" 添加到“@NgModule.schemas”以避免错误。通过将 "CUSTOM_ELEMENTS_SCHEMA" 添加到您的模块,您基本上是在告诉 Angular 让您向应用程序添加自定义元素。
因此,在您的根模块中,将其添加到导入中:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
然后,在同一个根模块中,在 "imports" 下面添加 "schemas",就像这个例子:
@NgModule({
imports: [ RouterModule, CommonModule, FormsModule ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
另外,请注意:您似乎在此处输入错误 - "name" 中缺少 "e":
console.log(this.valueToPass.nam);
我一直在寻找 SO 来解决我的问题,我知道它被问了很多次,但它对我的问题没有用。
我有两个组件:
部门
全布局
整个问题是尝试使用 @Inputs()
.
下面我将先向您展示我的文件夹结构,然后是我的代码。
结构Department Folder Structure
Layout-Folder Structure
departments.component.ts
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'app-departments',
templateUrl: './departments.component.html',
inputs: ['valueToPass']
})
export class DepartmentsComponent implements OnInit {
@Input() valueToPass;
public _departments;
constructor () { }
ngOnInit() {
console.log(this.valueToPass.nam);
}
}
departments.module.ts
// import{...}...
// assume necessary imports above
@NgModule({
imports: [
DepartmentsRoutingModule,
CommonModule,
MaterialModule.forRoot()
],
declarations: [ DepartmentsComponent ]
})
export class DepartmentsModule {}
full-layout.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './full-layout.component.html',
})
export class FullLayoutComponent implements OnInit {
public disabled:boolean = false;
public status:{isopen:boolean} = {isopen: false};
constructor (){}
ngOnInit (): void {}
dropDownItems = [
{ routerLink: '/departments', name: 'Artshums' },
{ routerLink: '/components/social-buttons', name: 'Dentistry' },
{ routerLink: '/components/cards', name: 'Law' },
{ routerLink: '/components/forms', name: 'IOPPN' },
{ routerLink: '/components/modals', name: 'LSM' },
{ routerLink: '/departments', name: 'NMS' },
{ routerLink: '/components/tables', name: 'Nursing' },
{ routerLink: '/components/tabs', name: 'SSPP' },
{ routerLink: '/components/tabs', name: 'Health' }
];
onClick(_container: HTMLElement) {
//this.clickedElement = _container.innerText;
//console.log(this.clickedElement);
}
}
full-layout.component.html
<ul class="nav-dropdown-items">
<li class="nav-item" *ngFor="let item of dropDownItems">
<a #clicked class="nav-link" routerLinkActive="active" [routerLink]="[item.routerLink]" (click)="onClick(clicked)">
<i class="icon-puzzle"></i>{{item.name}}
<app-departments [valueToPass] = "item"></app-departments>
</a>
</li>
</ul>
app.module.ts
// import {...}...
// assume necessary imports
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
DropdownModule.forRoot(),
TabsModule.forRoot(),
ChartsModule,
MaterialModule.forRoot(),
HttpModule,
JsonpModule,
DepartmentsModule
],
declarations: [
AppComponent,
FullLayoutComponent,
SimpleLayoutComponent,
NAV_DROPDOWN_DIRECTIVES,
BreadcrumbsComponent,
SIDEBAR_TOGGLE_DIRECTIVES,
AsideToggleDirective
],
providers: [{
provide: LocationStrategy,
useClass: HashLocationStrategy
}],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的问题就出在这里。当我 运行 这个程序时,我遇到了绑定错误,我不确定为什么以及如何修复它。
无法绑定到 'valueToPass',因为它不是 'app-departments' 的已知 属性。
如果 'app-departments' 是一个 Angular 组件并且它有 'valueToPass' 输入,然后验证它是该模块的一部分。
如果 'app-departments' 是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到此组件的“@NgModule.schemas”以禁止显示此消息。
如果能提供小小的帮助,我们将不胜感激。
谢谢冠军!
当您使用自定义选择器时,就像您使用这个:<app-departments>
,有时您需要将 "CUSTOM_ELEMENTS_SCHEMA" 添加到“@NgModule.schemas”以避免错误。通过将 "CUSTOM_ELEMENTS_SCHEMA" 添加到您的模块,您基本上是在告诉 Angular 让您向应用程序添加自定义元素。
因此,在您的根模块中,将其添加到导入中:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
然后,在同一个根模块中,在 "imports" 下面添加 "schemas",就像这个例子:
@NgModule({
imports: [ RouterModule, CommonModule, FormsModule ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
另外,请注意:您似乎在此处输入错误 - "name" 中缺少 "e":
console.log(this.valueToPass.nam);