【问题标题】:Errors parsing template: Unexpected closing tag app-post-list解析模板时出错:意外的结束标记 app-post-list
【发布时间】:2020-07-13 16:35:14
【问题描述】:

当我尝试以角度输出我的帖子时出现错误。请帮忙解决这个问题...

错误信息

解析模板时出错:意外的结束标记“app-post-list”。

我的 post-list.component.ts 代码

import { Component } from "@angular/core";

@Component({
selector: 'aap-post-list',
templateUrl: './post-list.component.html'
})

export class PostListComponent {}

我的 post-list.component.html

<mat-accordion>
   <mat-expansion-panel>
    <mat-expansion-panel-header>
      The expansion title!
    </mat-expansion-panel-header>
    <p>I'm in an expansion panel!</p>
   </mat-expansion-panel>
</mat-accordion>

app.component.html 代码

<app-header></app-header>
<main>
  <app-post-create></app-post-create>
  <aap-post-list></app-post-list>
</main>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatInputModule} from '@angular/material/input';
import { MatCardModule} from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatExpansionModule } from '@angular/material/expansion';
import { AppComponent } from './app.component';
import {PostCreateComponent} from './posts/post-create/post-create.component';
import { HeaderComponent } from './header/header.component';
import { PostListComponent } from './posts/post-list/post-list.component';
import { from } from 'rxjs';

@NgModule({
declarations: [
AppComponent,
PostCreateComponent,
HeaderComponent,
PostListComponent
],
imports: [
BrowserModule,
FormsModule,
BrowserAnimationsModule,
MatInputModule,
MatCardModule,
MatButtonModule,
MatToolbarModule,
MatExpansionModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { PostListComponent } from './posts/post-list/post-list.component';

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

it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});

it(`should have as title 'mean-learning'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('mean-learning');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('mean-learning app is running!');
});
});

请让我知道我错在哪里以及如何解决这个问题。

【问题讨论】:

  • 这是一个错字。在 app.component.html 中,将 &lt;aap-post-list&gt;&lt;/app-post-list&gt; 替换为 &lt;aap-post-list&gt;&lt;/aap-post-list&gt;。更好的是,将组件选择器更改为app-post-list
  • 如果问题解决请点击答案上的已解决图标
  • 现在我收到此错误消息 src/app/app.component.html:4:5 - error NG8001: 'app-post-list' is not a known element: If 'app-post- list' 是一个 Angular 组件,然后验证它是这个模块的一部分。如果“app-post-list”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。 src/app/app.component.ts:5:16 templateUrl: './app.component.html', 组件AppComponent的模板出错。

标签: node.js angular mean


【解决方案1】:

您好,您在 4 的行号中做了 Typo,将 &lt;aap-post-list&gt; 更改为 &lt;app-post-list&gt; 问题已解决。

app.component.html 代码

<app-header></app-header>
<main>
  <app-post-create></app-post-create>
  <aap-post-list></app-post-list>
</main>

更新

你已经改变了&lt;aap-post-list&gt; to &lt;app-post-list&gt;所以现在回到

post-list.component.ts

现在将 选择器 更改为

从“@angular/core”导入{组件};

@Component({
selector: 'aap-post-list',
templateUrl: './post-list.component.html'
})

export class PostListComponent {}

从“@angular/core”导入{组件};

@Component({
selector: 'app-post-list',
templateUrl: './post-list.component.html'
})

export class PostListComponent {}

现在应该可以正常使用了

【讨论】:

  • 先生,谢谢你的回答我改变了它,但现在我收到这条消息 app-post-list' is not a known element 如果'app-post-list' 是一个 Angular 组件,那么验证它是这个模块的一部分。如果“app-post-list”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。
  • 您将组件声明为aap-post-list。所以你也应该改变声明(选择器)。
  • 将选择器:'aap-post-list',改为'app-post-list'
  • @NaveenKumar 对不起,我没有得到你的答案,请解释一下
  • @ayushflitzip 现在检查我的答案
猜你喜欢
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 2021-06-30
  • 2015-08-20
  • 1970-01-01
相关资源
最近更新 更多