加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如果”是Angular组件,则验证它是否是此模块的一部分

发布时间:2020-12-17 17:34:13 所属栏目:安全 来源:网络整理
导读:我在使用Angular的组件时遇到了麻烦.我的应用程序只使用一个模块(app.module).我创建了一个名为’BooksComponent’的组件,其中包含一个用于html的选择器’app-books’.当我在app.component中使用它时,我收到此错误: ‘app-books’ is not a known element:
我在使用Angular的组件时遇到了麻烦.我的应用程序只使用一个模块(app.module).我创建了一个名为’BooksComponent’的组件,其中包含一个用于html的选择器’app-books’.当我在app.component中使用它时,我收到此错误:

‘app-books’ is not a known element:
1. If ‘app-books’ is an Angular component,then verify that it is part of this module.
2. If ‘app-books’ is a Web Component then add ‘CUSTOM_ELEMENTS_SCHEMA’ to the ‘@NgModule.schemas’ of this component to suppress this message.

我的代码是这样的:

books.component.ts

import { Component,OnInit } from '@angular/core';
import { Book } from '../book';
import { BOOKS } from '../mock-books';


@Component({
  selector: 'app-books',templateUrl: './books.component.html',styleUrls: ['./books.component.css']
})
export class BooksComponent implements OnInit {

        books = BOOKS;
        selectedBook : Book;

  constructor() { }

  ngOnInit() {
  }

}

app.component.ts

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


@Component({
  selector: 'app-root',template: `<h1>{{title}}</h1>
            <app-books></app-books>`
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'BookShelf';
}

最后:
app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';


import { AppComponent } from './app.component';
import { BooksComponent } from './books/books.component';
import { BookDetailComponent } from './book-detail/book-detail.component';


@NgModule({
  declarations: [
    AppComponent,BooksComponent,BookDetailComponent,],imports: [
    BrowserModule,FormsModule
  ],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }

我已经在app模块中声明并导入BooksComponent,所以我无法理解我错过了什么!请帮忙!

解决方法

如果在运行测试时遇到错误,则可能意味着规范需要了解新组件BooksComponent.

要解决此问题,请将以下内容添加到app.component.spec.ts文件中.

import { BooksComponent } from './books/books.component';

在beforeEach()方法的声明中,添加如下所示的新组件:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读