Angular 角度错误连接在http请求时超时
嗨,我是Angular的新手,正在尝试创建一个使用Spotify API的应用程序。当我使用搜索组件进行搜索时,但什么也没有发生,我在控制台中收到一个错误 获取网络::错误\u连接\u超时\u 下面是包含服务的位置service.tsAngular 角度错误连接在http请求时超时,angular,angular2-services,angular-http,Angular,Angular2 Services,Angular Http,嗨,我是Angular的新手,正在尝试创建一个使用Spotify API的应用程序。当我使用搜索组件进行搜索时,但什么也没有发生,我在控制台中收到一个错误 获取网络::错误\u连接\u超时\u 下面是包含服务的位置service.ts import {Injectable} from '@angular/core'; import {Http, Headers} from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable(
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class SpotifyService{
private searchUrl:string;
constructor(private _http:Http){
}
searchMusic(str:string, type="artist"){
this.searchUrl = `https://api.spotfiy.com/v1/search?query=${str}&offset=0&limit=20&type=${type}&market=US`;
return this._http.get(this.searchUrl).map(res => res.json());
}
}
搜索组件
import { Component } from '@angular/core';
import {SpotifyService} from '../../services/spotify.service';
@Component({
moduleId: module.id,
selector: 'search',
templateUrl: 'search.component.html',
//providers:[SpotifyService]
})
export class SearchComponent{
searchStr:string;
constructor(private _spotifyService:SpotifyService){
}
searchMusic(){
this._spotifyService.searchMusic(this.searchStr).subscribe(res => {
console.log(res.artist.items);
})
}
}
应用程序模块
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { RouterModule, Routes } from '@angular/router';
import { NavbarComponent } from './components/navbar/navbar.component';
import { AboutComponent } from './components/about/about.component';
import { SearchComponent } from './components/search/search.component';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { SpotifyService } from './services/spotify.service';
const routes: Routes = [
{ path: '', component: SearchComponent },
{ path: 'about', component: AboutComponent }
]
@NgModule({
imports: [ BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(routes) ],
declarations: [ AppComponent, AboutComponent, NavbarComponent, SearchComponent ],
providers: [ SpotifyService ],
bootstrap: [ AppComponent ],
})
export class AppModule { }
你的URL有一个输入错误。您无意中键入了spotfiy,而不是spotify。发生了:)