我正在尝试获取带有工作按钮的网页,以使用该按钮发送http请求。我在模块中安装并声明了angular http。这是我得到的错误
11 | @Injectable()
12 | export class LoadButton {
> 13 | constructor(private http: Http) {
| ^
14 | }
15 | clicked(event) {
16 | console.log(this.http.get)
npm ERR! Linux 4.4.0-38-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "build"
npm ERR! node v6.7.0
npm ERR! npm v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! random-quote@1.1.1 build: `babel src -d app`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the random-quote@1.1.1 build script 'babel src -d app'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the random-quote package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel src -d app
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs random-quote
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls random-quote
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /home/drew/Dropbox/js/sideProjs/redditTime/npm-debug.log
导致错误的文件是
import { Component } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Component ({
selector: 'buttonTest',
template: `
<button (click)='clicked($event)'>Test Button</button>
`
})
@Injectable()
export class LoadButton {
constructor(private http: Http) {
}
clicked(event) {
console.log(this.http.get)
}
}
在一些堆栈溢出帖子中提到了系统配置,但它似乎不正确
(function() {
// map tells the System loader where to look for things
var map = {
'app': 'app',
'@angular': 'node_modules/@angular',
'rxjs': 'node_modules/rxjs'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' }
};
var ngPackageNames = [
'common',
'compiler',
'core',
'forms',
'http',
'platform-browser',
'platform-browser-dynamic',
'router'
];
ngPackageNames.forEach(function(pkgName) {
packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
});
var config = {
map: map,
packages: packages
};
System.config(config);
})();
有什么想法吗?不确定我错过了什么导致了这个错误。
答案 0 :(得分:0)
你的角度版本是什么?
在NgModule声明中导入HttpModule
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {HttpModule} from '@angular/http';
import {AppComponent} from './app.component';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpModule,
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
}