使用 Aurelia,有没有办法在客户端存储数据以便在视图 类 之间重用?
With Aurelia, is there a way to store data clientside for reuse between view classes?
我有一些在多个视图之间访问的数据。如果有人想分享那个?在 Angular 中,我能够将内容存储在根作用域或父控制器上,然后它对所有 sub-views/controllers 都可用。
我认为没有理由继续获取它们。
Services/classes 被创建为单例(除非你另外告诉 DI,我相信)所以使用服务 class/module 并将其注入你的视图控制器。
然后服务 class 可以使用内部缓存数据,或者它可以依赖于 http 模块并根据需要获取数据。
编辑:添加了一些示例:
我怀疑这是否会直接起作用,但它应该给出了基本思路。
全球服务"someGlobalStuff.js":
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject(HttpClient)
export class SomeGlobalStuff {
constructor(http) {
this.http = http;
}
getSomethingVital() {
if (this.somethingVital) {
return Promise.resolve(this.somethingVital)
} else {
// Do something with the HTTP client that will get the
// required stuff and return a promise
return this.http.get(blah blah blah)
.then(r => {
this.somethingVital = r;
return r; //
});
}
}
}
以及使用它的东西:
import {inject} from 'aurelia-framework';
import {SomeGlobalStuff} from 'someGlobalStuff';
@inject(SomeGlobalStuff)
export class DataManager {
constructor(someGlobalStuff) {
this.globalStuff = someGlobalStuff;
}
doSomething() {
this.globalStuff.getSomethingVital()
.then(v => {
// Do something with it
})
}
}
我有一些在多个视图之间访问的数据。如果有人想分享那个?在 Angular 中,我能够将内容存储在根作用域或父控制器上,然后它对所有 sub-views/controllers 都可用。
我认为没有理由继续获取它们。
Services/classes 被创建为单例(除非你另外告诉 DI,我相信)所以使用服务 class/module 并将其注入你的视图控制器。
然后服务 class 可以使用内部缓存数据,或者它可以依赖于 http 模块并根据需要获取数据。
编辑:添加了一些示例:
我怀疑这是否会直接起作用,但它应该给出了基本思路。
全球服务"someGlobalStuff.js":
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject(HttpClient)
export class SomeGlobalStuff {
constructor(http) {
this.http = http;
}
getSomethingVital() {
if (this.somethingVital) {
return Promise.resolve(this.somethingVital)
} else {
// Do something with the HTTP client that will get the
// required stuff and return a promise
return this.http.get(blah blah blah)
.then(r => {
this.somethingVital = r;
return r; //
});
}
}
}
以及使用它的东西:
import {inject} from 'aurelia-framework';
import {SomeGlobalStuff} from 'someGlobalStuff';
@inject(SomeGlobalStuff)
export class DataManager {
constructor(someGlobalStuff) {
this.globalStuff = someGlobalStuff;
}
doSomething() {
this.globalStuff.getSomethingVital()
.then(v => {
// Do something with it
})
}
}