角度未捕获错误:无法解析服务的所有参数

问题描述 投票:1回答:2

我不明白为什么这项服务不起作用?这是依赖关系的一些问题吗?

服务:

import { Http } from '@angular/http';

export class GetWeatherService {

    constructor(private http: Http) {}

    getWeather() {
        return this.http.get(link);
    }
}

和组件:

import { Component, OnInit } from '@angular/core';
import { GetWeatherService } from './get-weather.service';

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

    constructor(public getWeather: GetWeatherService) {}

    ngOnInit() {
        this.getWeather.getWeather();
    }
}
angular typescript
2个回答
4
投票

您需要使您的服务可注射:

@Injectable()
export class GetWeatherService {
    //CODE
}

2
投票

在服务上添加@Injectable(),将其注入构造函数中。

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

@Injectable()
export class GetWeatherService {

    constructor(private http: Http) {}

    getWeather() {
        return this.http.get(link);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.