Если я использую es6/7 (babel - stage 1) вместо TypeScript, как вставляются услуги и, в частности, Http?
Здесь мой компонент JS:
import {Component, Inject, View, CORE_DIRECTIVES, ViewEncapsulation} from 'angular2/angular2';
import {Http} from 'angular2/http';
@Component({
selector: 'login'
})
@View({
templateUrl: './components/login/login.html',
styleUrls: ['components/login/login.css'],
directives: [CORE_DIRECTIVES],
encapsulation: ViewEncapsulation.Emulated
})
export class Login {
constructor(@Inject(Http) http) {
console.log('http', http);
}
authenticate(username, password) {
// this.http.get('/login');
}
}
Я пробовал:
export class Login {
constructor(@Inject(Http) http) {
console.log('http', http);
}
}
/********************/
@Inject(Http)
export class Login {
constructor(http) {
console.log('http', http);
}
}
/********************/
export class Login {
constructor(Http: http) {
console.log('http', http);
}
}
/********************/
export class Login {
constructor(http = Http) {
console.log('http', http);
}
}
/********************/
export class Login {
constructor(Http) {
this.http = new Http()
console.log('http', this.http);
}
}
/********************/
export class Login {
constructor(http = new Http()) {
console.log('http', http);
}
}
Все, кроме первого компиляции. Другие дают мне доступ к классу Http или к экземпляру http. Но никто не работает.
Я попытался после обсуждения, упомянутого Эриком Мартинесом в его комментарии. Login.js:
import {Component, Inject, View, CORE_DIRECTIVES, ViewEncapsulation} from 'angular2/angular2';
import {HTTP_BINDINGS, Http, BaseRequestOptions, RequestOptions, RequestMethods} from 'angular2/http';
@Component({
selector: 'login'
})
@View({
templateUrl: './components/login/login.html',
styleUrls: ['components/login/login.css'],
directives: [CORE_DIRECTIVES],
encapsulation: ViewEncapsulation.Emulated,
bindings: [Http]
})
export class Login {
constructor(http) {
this.http = http;
console.log('http', http);
}
authenticate(usernameEl, passwordEl) {
var username = usernameEl.value;
var password = passwordEl.value;
console.log('username', username, password);
// this.http.get('/login');
}
}
Login.parameters = [Http];
Теперь он компилируется, но генерирует следующую ошибку:
Неподготовлено (в обещании) NoBindingError {сообщение: "Нет провайдера для Http! (Login → Http)", стек: "Ошибка: исключение DI" в NoBindingError.BaseExce... or._new (http://localhost:3000/bundle.js:7319:22) ", keys: Array [2], инжекторы: Array [2]} constructResolvingMessage: (keys) arguments: (...) caller: (...) length: 1name:" " prototype: Object__proto__:() context: (...) injectors: Array [2] 0: Injector1: Injectorlength: 2__proto__: Array [0] keys: Array [2] сообщение: "Нет провайдера для Http! (Login → Http)" stack:" Ошибка: DI Exception↵ at NoBindingError.BaseException [как конструктор] (http://localhost:3000/bundle.js:8400:24) ↵ при NoBindingError.AbstractBindingError [как конструктор] (http://localhost:3000/bundle.js:9066:17) ↵ при новом NoBindingError (http://localhost:3000/bundle.js:9102:17) ↵ на Injector._throwOrNull (http://localhost:3000/bundle.js:7469:20) ↵ при Injector._getByKeyDefault (http://localhost:3000/bundle.js:7516:22) ↵
в Injector._getByKey (http://localhost:3000/bundle.js:7461:26) ↵ при Injector._getByDependency (http://localhost:3000/bundle.js:7447:26) ↵
в Injector._instantiate (http://localhost:3000/bundle.js:7339:37) ↵
в Injector._instantiateBinding (http://localhost:3000/bundle.js:7330:26) ↵ на Injector._new (http://localhost:3000/bundle.js:7319:22)" Прото: __