[英]share data between two files through es6 class in mithril js or share instance of es6 class
我想在mithril js中的兩個文件之間共享數據,使用webpack進行模塊加載,我有這個類,我從ajax請求中獲取數據,然后將其存儲在下面給出的類成員中。
import m from 'mithril';
import { myClass } from './model';
let asad=new myClass();
var Data = {
fetch: function() {
m.request({
method: "GET",
url: "./config/config.json",
})
.then(function(items) {
asad.setConfig(items) // storing this data here .
m.route(document.body, "/accounts", routes)
})
}
}
Data.fetch()
這是我的課文件
export class myClass {
constructor() {
}
setConfig(obj) {
this.config= obj
}
getConfig() {
return this.config;
}
}
在另一個文件中,我想獲取此數據
import m from 'mithril';
import { myClass } from '../model';
export function Accounts() {
console.log('accon')
let asad=new myClass();
console.log(asad.getConfig())
}
問題是它創建了類的新實例,我如何共享該類的實例,所以可行
我通過使用以下代碼在es6中創建單例類來完成此操作。
export class globals {
constructor() {
if (!globals.instance) {
globals.instance = this
}
return globals.instance;
}
setConfig(obj) {
this.config= obj
}
getConfig() {
return this.config;
}
}
然后我調用get config方法可以正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.