在Angular 2中使用嵌套组件

时间:2016-10-05 15:28:38

标签: angular typescript

我有一个声明了几个组件的模块,当我在模板中使用指令语法时,角度找不到组件 - 我收到此错误

'test-cell-map' is not a known element:
  1. 如果' test-cell-map'是一个Angular组件,然后验证它是否是该模块的一部分。
  2. 如果' test-cell-map'是一个Web组件,然后添加" CUSTOM_ELEMENTS_SCHEMA"到' @ NgModule.schema'该组件可以禁止显示此消息。
  3. 这是组件模块

    import { BrowserModule } from '@angular/platform-browser';                                                                                                                                                                                    
    import { NgModule } from '@angular/core';                                                                                                                                                                                                     
    import { FormsModule } from '@angular/forms';                                                                                                                                                                                                 
    import { HttpModule } from '@angular/http';                                                                                                                                                                                                   
    
    import { AppComponent } from './app.component';                                                                                                                                                                                               
    import { TestCellMapComponent } from './test-cell-map/test-cell-map.component';                                                                                                                                                               
    
    @NgModule({                                                                                                                                                                                                                                   
        declarations: [                                                                                                                                                                                                                           
            AppComponent,                                                                                                                                                                                                                         
            TestCellMapComponent                                                                                                                                                                                                                  
        ],                                                                                                                                                                                                                                        
        imports: [                                                                                                                                                                                                                                
            BrowserModule,                                                                                                                                                                                                                        
            FormsModule,                                                                                                                                                                                                                          
            HttpModule                                                                                                                                                                                                                            
        ],                                                                                                                                                                                                                                        
        providers: [],                                                                                                                                                                                                                            
        bootstrap: [AppComponent]                                                                                                                                                                                                                 
    })                                                                                                                                                                                                                                            
    export class AppModule { }                                                                                                                                                                                                                    
    

    这是顶级组件的样子

    import { Component } from '@angular/core';                                                                                                                                                                                                    
    
    @Component({                                                                                                                                                                                                                                  
        selector: 'my-app',                                                                                                                                                                                                                       
        templateUrl: './app.component.html',                                                                                                                                                                                                      
        styleUrls: ['./app.component.css']                                                                                                                                                                                                        
    
    })                                                                                                                                                                                                                                            
    export class AppComponent {                                                                                                                                                                                                                   
      title = 'app works!';                                                                                                                                                                                                                       
    }
    

    及其相关模板

      <h1>                                                                                                                                                                                                                                          
        {{title}}                                                                                                                                                                                                                                   
        <test-cell-map></test-cell-map>                                                                                                                                                                                                             
     </h1>
    

1 个答案:

答案 0 :(得分:1)

你有 TestCellMapComponent 选择器

的问题
<h1>                                                                                                                                                                                                                                          
  {{title}}                                                                                                                                                                                                                                   
  <app-test-cell-map></app-test-cell-map>      //<<<<===== changed                                                                                                                                                                                                                                                   
</h1>
相关问题