Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 错误:StaticInjectorError[字符串]_Angular - Fatal编程技术网

Angular 错误:StaticInjectorError[字符串]

Angular 错误:StaticInjectorError[字符串],angular,Angular,我是一个试图学习angular 5的noob,我试图做一个简单的项目,对api进行ajax操作并显示结果,所有这些都很好,直到我添加了一个新组件(buscador)和一个新服务(buscador.service.ts)。代码如下: buscador.component.html <div class="buscador"> <form> <label for="ciutat">Ciudad:</label>

我是一个试图学习angular 5的noob,我试图做一个简单的项目,对api进行ajax操作并显示结果,所有这些都很好,直到我添加了一个新组件(buscador)和一个新服务(buscador.service.ts)。代码如下:

buscador.component.html

<div class="buscador">
    <form>
        <label for="ciutat">Ciudad:</label>
        <input type="text" #city>
        <button (click)="enterData(city.value)">Buscar</button>
    </form>
</div>
<div class="poblacion row">
    <div class="header column">
        <h4 *ngIf="resposta">{{resposta.location.name}} - {{resposta.location.region}}</h4>
    </div>
    <div class="info column">
        <ul>
            <li *ngIf="!resposta">Cargando...</li>
            <li *ngIf="resposta">Temperatura actual: {{resposta.current.temp_c}}</li>
            <li *ngIf="resposta">Sensacion termica: {{resposta.current.feelslike_c}}</li>
            <li *ngIf="resposta">Condicion: {{resposta.current.condition.text}}</li>
        </ul>
    </div>
</div>
buscador.service.ts

import { Component, OnInit } from '@angular/core';
import { BuscadorService } from './buscador.service';
@Component({
    selector: 'app-buscador',
    templateUrl: 'buscador.component.html'
})

export class Buscador{
    constructor(private value: BuscadorService){}
    enterData(city){
        this.value.setValue(city);
        console.log(city);
    }
}
import { Injectable } from '@angular/core';

@Injectable()
export class BuscadorService {
  constructor(private value: string) { }

  setValue(newValue) {
    this.value = newValue;
    console.log("insertado");
  }

  getValue(){
    return this.value;
  }
}
import { Component } from '@angular/core';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
import { FetchService } from './fetch.service'; 
import { Buscador } from '../buscador/buscador.component';
import { BuscadorService } from '../buscador/buscador.service';

@Component({
    selector: "app-fetch",
    templateUrl: "./fetch.component.html",
    styleUrls: ["./fetch.component.css"]
})
export class Fetch implements OnInit  {
    private url = 'http://api.apixu.com/v1/current.json?key=a456c65a89b44bf8ac6101713161207&q=Girona';
    resposta: any;

    constructor(private http: FetchService,private value:BuscadorService){}



    ngOnInit() {
        var newCity = this.value.getValue();
            if (newCity == null || newCity === 'undefined'){
            this.http.getDataObservable(this.url).subscribe(
                data => this.resposta = data
            );
        }else{
            var urlCity = "http://api.apixu.com/v1/current.json?key=a456c65a89b44bf8ac6101713161207&q=" + newCity;
            this.http.getDataObservable(urlCity).subscribe(
                data => this.resposta = data
            )
        }

        console.log("init correcto!");
    }
}
import { HttpClientModule, HttpClient } from '@angular/common/http'
import { HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/retry';
import 'rxjs/add/operator/map';

// API: https://www.apixu.com/api-explorer.aspx

@Injectable()
export class FetchService {
  constructor(private http:HttpClient){}
  //Metodo que buscara la ainfo a la api, por parametro recibe la url de la api
  getDataObservable(url:string) {
    return this.http.get(url)
      //Creamos un mapa apartir de los datos recibidos
        .map(data => {
          console.log("Todo correcto! ", data);
          return data;
        },
      //Cazamos errores
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error){
              console.log('Error: ',err.error.message)
          }else{
              console.log(`Status: ${err.status}, Error: ${err.error}`);
          }
      });
  }
}
fetch.component.html

<div class="buscador">
    <form>
        <label for="ciutat">Ciudad:</label>
        <input type="text" #city>
        <button (click)="enterData(city.value)">Buscar</button>
    </form>
</div>
<div class="poblacion row">
    <div class="header column">
        <h4 *ngIf="resposta">{{resposta.location.name}} - {{resposta.location.region}}</h4>
    </div>
    <div class="info column">
        <ul>
            <li *ngIf="!resposta">Cargando...</li>
            <li *ngIf="resposta">Temperatura actual: {{resposta.current.temp_c}}</li>
            <li *ngIf="resposta">Sensacion termica: {{resposta.current.feelslike_c}}</li>
            <li *ngIf="resposta">Condicion: {{resposta.current.condition.text}}</li>
        </ul>
    </div>
</div>
fetch.service.ts

import { Component, OnInit } from '@angular/core';
import { BuscadorService } from './buscador.service';
@Component({
    selector: 'app-buscador',
    templateUrl: 'buscador.component.html'
})

export class Buscador{
    constructor(private value: BuscadorService){}
    enterData(city){
        this.value.setValue(city);
        console.log(city);
    }
}
import { Injectable } from '@angular/core';

@Injectable()
export class BuscadorService {
  constructor(private value: string) { }

  setValue(newValue) {
    this.value = newValue;
    console.log("insertado");
  }

  getValue(){
    return this.value;
  }
}
import { Component } from '@angular/core';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
import { FetchService } from './fetch.service'; 
import { Buscador } from '../buscador/buscador.component';
import { BuscadorService } from '../buscador/buscador.service';

@Component({
    selector: "app-fetch",
    templateUrl: "./fetch.component.html",
    styleUrls: ["./fetch.component.css"]
})
export class Fetch implements OnInit  {
    private url = 'http://api.apixu.com/v1/current.json?key=a456c65a89b44bf8ac6101713161207&q=Girona';
    resposta: any;

    constructor(private http: FetchService,private value:BuscadorService){}



    ngOnInit() {
        var newCity = this.value.getValue();
            if (newCity == null || newCity === 'undefined'){
            this.http.getDataObservable(this.url).subscribe(
                data => this.resposta = data
            );
        }else{
            var urlCity = "http://api.apixu.com/v1/current.json?key=a456c65a89b44bf8ac6101713161207&q=" + newCity;
            this.http.getDataObservable(urlCity).subscribe(
                data => this.resposta = data
            )
        }

        console.log("init correcto!");
    }
}
import { HttpClientModule, HttpClient } from '@angular/common/http'
import { HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/retry';
import 'rxjs/add/operator/map';

// API: https://www.apixu.com/api-explorer.aspx

@Injectable()
export class FetchService {
  constructor(private http:HttpClient){}
  //Metodo que buscara la ainfo a la api, por parametro recibe la url de la api
  getDataObservable(url:string) {
    return this.http.get(url)
      //Creamos un mapa apartir de los datos recibidos
        .map(data => {
          console.log("Todo correcto! ", data);
          return data;
        },
      //Cazamos errores
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error){
              console.log('Error: ',err.error.message)
          }else{
              console.log(`Status: ${err.status}, Error: ${err.error}`);
          }
      });
  }
}

我认为在您的
buscadors.service.ts
中,您试图通过将字符串值作为参数提供给构造函数来声明它。这不起作用,构造函数参数用于依赖项注入(如FetchService中的Http注入)。这就是问题所在

constructor(private value: string) { }
您应该在构造函数之外声明此字符串

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

@Injectable()
export class BuscadorService {
  private value: string;
  constructor() { }

  setValue(newValue) {
    this.value = newValue;
    console.log("insertado");
  }

  getValue(){
    return this.value;
  }
}

您是否已将
buscador.service.ts
放入模块的提供程序数组中?是的,它已添加到提供程序中