简体   繁体   English

在Angular2中的两个组件之间传递数据会产生绑定错误

[英]Passing Data Between Two Components In Angular2 Produces Binding Error

I've been searching SO to resolve my issue, I know its been asked so many times but its no use for my problem. 我一直在寻找解决问题的方法,我知道它被问过很多次了,但是对我的问题没有用。

I have two components: 我有两个组成部分:

  • departments 部门

  • full-layout 全布局

The whole issue is trying to pass data from full-layout component to departments component using @Inputs() . 整个问题试图使用@Inputs()将数据从完整布局的组件传递到部门组件。

Below I'll show you my folder structure first and then the code that I have. 在下面,我将首先向您显示我的文件夹结构,然后向您显示我的代码。

The Structure 结构

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 { } 

My problem lies here. 我的问题就在这里。 When I run this program, Im getting binding error, and Im not sure why and how to fix it. 当我运行该程序时,我收到绑定错误,并且我不确定为什么以及如何修复它。

Can't bind to 'valueToPass' since it isn't a known property of 'app-departments'. 无法绑定到“ valueToPass”,因为它不是“应用程序部门”的已知属性。

  1. If 'app-departments' is an Angular component and it has 'valueToPass' input, then verify that it is part of this module. 如果“ app-departments”是Angular组件,并且具有“ valueToPass”输入,则请验证它是否属于此模块。

  2. If 'app-departments' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. 如果“ app-departments”是Web组件,则将“ CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“ @ NgModule.schemas”以禁止显示此消息。

A small help would be highly appreciated it. 一个小的帮助将不胜感激。

Thanks Champs! 谢谢冠军!

When you use custom selectors like you are with this one: <app-departments> , you sometimes need to add "CUSTOM_ELEMENTS_SCHEMA" to '@NgModule.schemas' to avoid errors. 当您像这样使用自定义选择器时: <app-departments> ,有时需要在“ @ NgModule.schemas”中添加“ CUSTOM_ELEMENTS_SCHEMA”,以避免错误。 By adding "CUSTOM_ELEMENTS_SCHEMA" to your module you're basically telling Angular to let you add custom elements to the app. 通过在模块中添加“ CUSTOM_ELEMENTS_SCHEMA”,您基本上是在告诉Angular让您向应用添加自定义元素。

So, in your root module, add this to the imports: 因此,在您的根模块中,将其添加到导入中:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

And then, in that same root module, add "schemas" below "imports" as well, like this example: 然后,在相同的根模块中,也将“方案”添加到“导入”下面,如下例所示:

@NgModule({
    imports: [ RouterModule, CommonModule, FormsModule ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})

Also, one note: it looks like you have a typo here - missing "e" in "name": 另外,请注意:您似乎在这里输入错误-在“名称”中缺少“ e”:

console.log(this.valueToPass.nam); 的console.log(this.valueToPass.nam);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM