模板解析错误Angular

问题描述 投票:0回答:1

我创建了新的角度项目和生成的组件。

ng new hello
ng g c sample

在应用程序中包含示例组件

app.compoment.ts

<app-sample></app-sample>

什么时候

ng test

运行,发生以下错误

AppComponent should create the app FAILED
        'app-sample' is not a known element:
        1. If 'app-sample' is an Angular component, then verify that it is part of this module.
        2. If 'app-sample' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<app-sample></app-sample>"): ng:///DynamicTestModule/AppComponent.html@0:0
        Error: Template parse errors:
            at syntaxError (node_modules/@angular/compiler/fesm5/compiler.js:2430:1)
            at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (node_modules/@angular/compiler/fesm5/compiler.js:20605:1)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (node_modules/@angular/compiler/fesm5/compiler.js:26171:1)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (node_modules/@angular/compiler/fesm5/compiler.js:26158:1)
            at node_modules/@angular/compiler/fesm5/compiler.js:26101:48
            at Set.forEach (<anonymous>)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (node_modules/@angular/compiler/fesm5/compiler.js:26101:1)
            at node_modules/@angular/compiler/fesm5/compiler.js:26019:1
            at Object.then (node_modules/@angular/compiler/fesm5/compiler.js:2421:33)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndAllComponents (node_modules/@angular/compiler/fesm5/compiler.js:26017:1)
        Error: Template parse errors:
        'app-sample' is not a known element:
        1. If 'app-sample' is an Angular component, then verify that it is part of this module.
        2. If 'app-sample' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<app-sample></app-sample>"): ng:///DynamicTestModule/AppComponent.html@0:0
            at syntaxError (node_modules/@angular/compiler/fesm5/compiler.js:2430:1)
            at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (node_modules/@angular/compiler/fesm5/compiler.js:20605:1)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (node_modules/@angular/compiler/fesm5/compiler.js:26171:1)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (node_modules/@angular/compiler/fesm5/compiler.js:26158:1)
            at node_modules/@angular/compiler/fesm5/compiler.js:26101:48
            at Set.forEach (<anonymous>)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (node_modules/@angular/compiler/fesm5/compiler.js:26101:1)
            at node_modules/@angular/compiler/fesm5/compiler.js:26019:1
            at Object.then (node_modules/@angular/compiler/fesm5/compiler.js:2421:33)
            at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndAllComponents (node_modules/@angular/compiler/fesm5/compiler.js:26017:1)

我尝试过几个提到here的解决方案

First try

创建样本模块

sample.module.ts

@NgModule({
  declarations: [ SampleComponent],
  exports: [ SampleComponent],
  imports: [ CommonModule ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class SampleModule {}

Second try

CUSTOM_ELEMENTS_SCHEMA添加到app模块本身

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    SampleComponent
  ],
  imports: [
    BrowserModule,,
    SampleModule
  ],
  exports: [SampleModule],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]

})
export class AppModule { }

Third try

添加CUSTOM_ELEMENTS_SCHEMA测试床配置

sample.component.spec.ts

describe('SampleComponent', () => {
  let component: SampleComponent;
  let fixture: ComponentFixture<SampleComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ SampleComponent ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA],
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SampleComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

但没有那些修复了我得到的错误

angular karma-jasmine
1个回答
0
投票

正如在JB Nizet的评论之后,测试投注配置工作。由于app尝试包含示例组件时发生错误,app.component.spec.ts应在架构中包含CUSTOM_ELEMENTS_SCHEMA

app.component.spec.ts

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
        ],
        schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.