0
我正在玩Aurelia簡介教程,並將其更改爲從自定義課程中進行休息調用。在我的課程中,我的方法調用中出現'http is undefined'錯誤,所以看起來Aurelia服務沒有被注入。如果Aurelia沒有加載類模塊,是否注入工作?Aurelia不在註冊圖書館課
home.js:
import 'bootstrap/css/bootstrap.min.css!';
import 'bootstrap/css/bootstrap-theme.min.css!';
import 'styles/style.css!'
import 'bootstrap';
import {Search} from '../lib/search.js';
export class Home {
heading = 'Welcome to Aurelia!';
firstName = 'John';
lastName = 'Doe';
activate() {
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
submit() {
var s = new Search();
s.fetch()
.then(results => this.results = results);
}
}
search.js:
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';
@inject(HttpClient)
export class Search {
constructor(http) {
// constructor is called but http is undefined
http.configure(config => {
config
.useStandardConfiguration();
});
this.http = http;
}
fetch() {
// ERROR OCCURS HERE
return this.http.fetch('find')
.then(response => response.json());
}
}
錯誤:
TypeError: http is undefined
儘管這個答案解決了我的錯誤,但它並沒有回答我爲什麼在'Search'類中注入'HttpClient'不起作用的問題。你可以解釋嗎? –
當你創建'新的搜索()'時,'Search'內部的注入不會被執行,因爲注入應該被aurelia引擎解析。要在'Search'裏面加入'HttpClient',只需要在'Home'中注入它,然後傳遞給Search'new Search(this.http) – valichek