簡體   English   中英

Angular 6服務注入異常

[英]Angular 6 service injection exception

兩天前我開始角度,我試圖創建一個服務,將在我的春季啟動休息終點上做一個獲取請求,我希望在我的角度應用程序中顯示結果

這是我到現在為止所嘗試的

我的服務:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ITweet } from './itweet';
import { Observable } from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class ReactiveTwitterService {

    constructor(private http_client: HttpClient, private tweetTag: string) { }
    spring_webflux_service_url = 'http://localhost:8081/search';

    myTweets: Observable<ITweet[]>;

    setTweetTag(tag) {
        this.tweetTag = tag;
    }

    seearchTweets() {
        this.myTweets = this.http_client.get<ITweet[]>(this.spring_webflux_service_url + '/' + this.tweetTag);
    }

    getTweets() {
        return this.myTweets;
    }

}

如你所見,我等待推文作為回應,所以這是我的推文界面

export interface ITweet {

    id: {
        text: string,
        name: string
    };
    tag: string;


}

我的app模塊看起來像這樣

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

import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SerachBarComponent } from './serach-bar/serach-bar.component';
import { SearchReasultComponent } from './search-reasult/search-reasult.component';
import { HeaderComponent } from './header/header.component';
import { ResultItemComponent } from './result-item/result-item.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    SerachBarComponent,
    SearchReasultComponent,
    ResultItemComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我用谷歌搜索,由於服務實現中的providedIn指令 ,不需要在提供程序中設置我的服務

我使用此服務的組件

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



@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    innerWidth: number;
    styleClass = {
        wide_screen: 'w3-light-grey',
        break_point: 'w3-dark-grey'
    };


    @HostListener('window:resize', ['$event'])
    onResize(event) {
        this.innerWidth = window.innerWidth;
    }

    getStyle() {
        return (innerWidth > 769) ? this.styleClass.wide_screen : this.styleClass.break_point;
    }
}

import { Component, OnInit, HostListener } from '@angular/core';
import { ReactiveTwitterService } from '../reactive-twitter.service';

@Component({
    selector: 'app-serach-bar',
    templateUrl: './serach-bar.component.html',
    styleUrls: ['./serach-bar.component.css']
})
export class SerachBarComponent implements OnInit {
    innerWidth: number;

    constructor(private twiterService: ReactiveTwitterService) { }

    placeholder = 'search';

    styleClass = {
        wide_screen: 'w3-input w3-light-grey',
        break_point: 'w3-input w3-white'
    };

    doSearch(tag) {
        this.twiterService.setTweetTag(tag);
        this.twiterService.seearchTweets();
    }


    ngOnInit() {
    }

    @HostListener('window:resize', ['$event'])
    onResize(event) {
        this.innerWidth = window.innerWidth;
    }

    getStyle() {
        return (innerWidth > 769) ? this.styleClass.wide_screen : this.styleClass.break_point;
    }
}

import { Component, OnInit, HostListener } from '@angular/core';
import { ReactiveTwitterService } from '../reactive-twitter.service';
import { ITweet } from '../itweet';

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

    search_result: ITweet[];
    innerWidth: number;
    constructor(private _twitterService: ReactiveTwitterService) { }
    styleClass = {
        wide_screen: 'w3-ul w3-hoverable',
        break_point: 'w3-green w3-container'
    };


    ngOnInit() {
        this._twitterService.getTweets().subscribe(tweet => this.search_result = tweet);
    }

    is_search_result_empty() {
        return this.search_result === [];
    }

    set_search_result_empty() {
        this.search_result = [];
    }

    @HostListener('window:resize', ['$event'])
    onResize(event) {
        this.innerWidth = window.innerWidth;
    }

    get_list_style() {
        return (innerWidth > 769) ? this.styleClass.wide_screen : this.styleClass.break_point;
    }

}

我的模板是

AppComponent

<div class="{{getStyle()}}" style="width: 100%;height: 100%;">
  <app-header></app-header>
  <app-serach-bar></app-serach-bar>
  <app-search-reasult></app-search-reasult>
</div>

搜索欄

<div class="w3-container w3-margin-top">
  <input class="{{getStyle()}}" type="text" placeholder="{{placeholder}}" (onclick.enter)="doSearch(searchinput.value)" #searchinput>
</div>

搜索結果

<div class="w3-container" *ngIf="!is_search_result_empty">
  <ul class="{{get_list_style()}}">
    <app-result-item *ngFor="let current_item of search_result; trackBy:current_item.id" [item]="current_item"></app-result-item>
  </ul>
</div>

控制台記錄異常,一切都是空白的

例外

我該怎么做才能解決這個問題?

你需要將服務添加到模塊中的提供者當然記得導入服務

    @NgModule({
      declarations: [
        AppComponent,
        HeaderComponent,
        SerachBarComponent,
        SearchReasultComponent,
        ResultItemComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        HttpClientModule,
        FormsModule
      ],
      providers: [
        ReactiveTwitterService 
      ],
      bootstrap: [AppComponent]
    })

在您的代碼constructor(private _twitterService: ReactiveTwitterService) { }中無法初始化private tweetTag: string因此它仍然失敗,並且@Injectable({ providedIn: 'root' })providers: [ReactiveTwitterService]行為不同providers: [ReactiveTwitterService]

您的服務應作為提供商提供給您的組件或模塊。 您可以將它添加到providers: appmodule中的數組或單個模塊,並將其注入組件以供使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM