Angular 6组件未显示

dav*_*ler 5 html angular6

我昨天才刚开始学习Angular,所以如果我缺少明显的东西,我深表歉意,但是我试图在app.component.html上显示一个组件,但是它没有出现。

我尝试显示的组件的TS文件:

import { Component, OnInit } from '@angular/core';
import { ImageService } from '../shared/image.service';

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

  images: any[];

  constructor(private _imageService : ImageService ) { }

  searchImages(query : string)
  {
    return this._imageService.getImage(query).subscribe
    (
      data => console.log(data),
      error => console.log(error),
      () => console.log("Request Completed!")
    );
  }

  ngOnInit() {
  } 
}
Run Code Online (Sandbox Code Playgroud)

image-list.component.html:

<button>Find Images</button>
Run Code Online (Sandbox Code Playgroud)

app.component.html:

<image-list></image-list>
Run Code Online (Sandbox Code Playgroud)

app.module.ts

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

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

image.service.ts

import { Injectable } from "@angular/core";
import { environment } from "../../environments/environment";
import { Http, Headers } from "@angular/http";
import { map, filter, scan } from 'rxjs/operators';

@Injectable()
export class ImageService
{
    private query: string;
    private API_KEY: string = environment.API_KEY;
    private API_URL: string = environment.API_URL;
    private URL: string = this.API_URL + this.API_KEY + '&q=';

    constructor(private _http: Http) {

    }

    getImage(query)
    {
        return this._http.get(this.URL + this.query).pipe(
            map((res) => res.json));
    }
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*tor 14

我在尝试使用模块外的组件时遇到了类似的问题。在这种情况下,您必须从.module.ts导出一个组件:

@NgModule({
    ...
    declarations: [ MyComponent ],
    exports: [ MyComponent ],
    ...
})
Run Code Online (Sandbox Code Playgroud)


fir*_*ves 2

您需要将组件和服务导入到 app.module.ts 中,然后导入到声明和提供者属性中

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

import { AppComponent } from './app.component';
import { ImageListComponent } from './image-list.component';
import { ImageService } from './image.service';

@NgModule({
  declarations: [
    AppComponent,
    ImageListComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ ImageService ],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

将 ImageListComponent 路径调整到导入语句中。

理论上,当您使用 Angular CLI 生成组件时,使用如下命令:

ng generate component image-list
Run Code Online (Sandbox Code Playgroud)

它应该为您更新您的 app.module.ts 文件。

生成服务使用

ng generate service image
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚遇到了同样的问题,并且我犯了一个(愚蠢的)错误,将组件放在index.html 中的 &lt;app-root&gt; 之外。 (2认同)