Angular 6组件没有显示出来

时间:2018-06-07 16:12:05

标签: 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() {
  } 
}

image-list.component.html:

<button>Find Images</button>

app.component.html:

<image-list></image-list>

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 { }

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));
    }
}

5 个答案:

答案 0 :(得分:1)

您需要将组件和服务导入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 { }

将ImageListComponent路径调整为import语句。

当您使用Angular CLI使用如下命令生成组件时,我们会逐步使用

ng generate component image-list

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

生成服务用途

ng generate service image

答案 1 :(得分:1)

选中这个

src / app / app.module.ts

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

通过此链接: https://malcoded.com/posts/why-angular-not-works/

答案 2 :(得分:0)

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

中导出组件。
@NgModule({
    ...
    declarations: [ MyComponent ],
    exports: [ MyComponent ],
    ...
})

答案 3 :(得分:0)

您可以从https://angular.io/start尝试Angular教程。它显示了如何在app.component.html页面上呈现自定义Ansible组件。

您只需要更新app.module.ts文件。假设您的组件名为ImageListComponent

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; // add this line

import { AppComponent } from './app.component';
import { ImageListComponent } from './image-list/image-list.component'; // add this line

@NgModule({
  declarations: [
    AppComponent,
    ImageListComponent // add this line
  ],
  imports: [
    BrowserModule,
    // add the following 3 lines
    RouterModule.forRoot([
      { path: '', component: ImageListComponent },
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

首先尝试此操作,而不添加任何自定义服务。

答案 4 :(得分:0)

似乎您没有任何东西可以触发searchImages中的方法ImageComponent上的API调用。在<button>Find Images</button>标签上单击click eventListener应该可以解决问题。还要在ImageComponent文件中注册ImageServiceapp.module.ts