Aurelia 没有注入库 class
Aurelia not injecting on library class
我正在玩 Aurelia 介绍教程并将其更改为从自定义 class 进行休息呼叫。在我的 class 中,我的方法调用出现 'http is undefined' 错误,因此似乎没有注入 Aurelia 服务。如果 Aurelia 没有加载 class 模块,注入是否有效?
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
必须在Home
中注入并且在导入
时最好不要使用.js
import {Search} from '../lib/search';
@inject(Search)
export class Home {
constructor(search) {
}
}
UPD:在这里你可以找到更多关于使用注入的不同方式的例子
UPD2:或者您可以手动将 HttpClient
实例传递给 Search
构造函数
import {HttpClient} from 'aurelia-fetch-client';
@inject(HttpClient)
export class Home {
constructor(http) {
this.http = http;
}
...
submit() {
var s = new Search(this.http);
...
}
}
我正在玩 Aurelia 介绍教程并将其更改为从自定义 class 进行休息呼叫。在我的 class 中,我的方法调用出现 'http is undefined' 错误,因此似乎没有注入 Aurelia 服务。如果 Aurelia 没有加载 class 模块,注入是否有效?
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
必须在Home
中注入并且在导入
.js
import {Search} from '../lib/search';
@inject(Search)
export class Home {
constructor(search) {
}
}
UPD:在这里
UPD2:或者您可以手动将 HttpClient
实例传递给 Search
构造函数
import {HttpClient} from 'aurelia-fetch-client';
@inject(HttpClient)
export class Home {
constructor(http) {
this.http = http;
}
...
submit() {
var s = new Search(this.http);
...
}
}