Angular 4服务构造函数出错

时间:2017-11-05 12:10:21

标签: angular typescript

我创建了一个名为MyDataService的服务

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

@Injectable()
export class MyDataServiceService {

  constructor(private http:Http) {}//The Http parameter  shows an error

  getData(){
    this.http.get('http://stats.nba.com/stats/leaguedashplayerbiostats/?PerMode=Totals&Season=2016-17&LeagueID=00&SeasonType=Playoffs').subscribe(
      (data) => console.log(data)
    )
  }

}

在te构造函数上,它在浏览器控制台上显示错误“找不到名称Http”错误为“错误:无法解析MyDataServiceService的所有参数:(?)。”

这是app.componnet.ts

import { Component } from '@angular/core';
import { MyDataServiceService } from './my-data-service.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  constructor ( private service:MyDataServiceService){}

  ngOnInit(){
    this.service.getData();
  }
}

和app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { MyDataServiceService} from './my-data-service.service';

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

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpModule
  ],
  providers: [MyDataServiceService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

如何在您的案例中使用http(新的HttpClient):

  1. 如果您还没有
  2. 在您的模块中导入HttpClientModule(可能是app.module.ts)
  3. 在您的服务中,import { HttpClientModule } from '@angular/common/http';
  4. 更改getData()方法以返回可观察对象。订阅应该在组件中而不在您的服务中:

    getData(){
        return this.http.get('http://stats.nba.com/stats/leaguedashplayerbiostats/?PerMode=Totals&Season=2016-17&LeagueID=00&SeasonType=Playoffs');
    }
    
  5. 然后在你的组件中:

    ngOnInit(){
       this.service.getData().subscribe((data) => console.log(data));;
    }
    

答案 1 :(得分:0)

试试这个,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="nl">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="https://use.fontawesome.com/0ba2bca7b4.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light navbar-custom z-depth-half">
    <div class="container">

      <a class="navbar-brand" href="#">
        <i class="fa fa-skype"></i>
      </a>

      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
       </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">

        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a href="#" class="nav-link">Vragen</a>
          </li>
          <li class="nav-item">
            <a href="#" class="nav-link">Aanbieders</a>
          </li>
        </ul>


        <ul class="navbar-nav">
          <li class="nav-item" style="border-right: 0.1rem solid lightgrey">
            <form class="form-inline">
              <input class="form-control mr-sm-2 search-input" type="search" placeholder="Zoeken..." name="search" id="search">
              <button class="btn btn-search my-2 my-sm-0 " type="submit">
                 <i class="fa fa-search"></i>
              </button>
            </form>

          </li>



          <li class="nav-item">
            <a class="nav-link" href="">Aanmelden</a>
          </li>
          <li class="nav-item">
            <a class="nav-link btn nav-register-btn" href="">Registreren</a>
          </li>

      
        </ul>
      </div>
    </div>
  </nav>
</body>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</html>