[英]share data between two files through es6 class in mithril js or share instance of es6 class
I want to share data between two files in mithril js Im using webpack for module loading I have this class , Im getting data from ajax request and then storing it in class member given below. 我想在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()
this is my class file 这是我的课文件
export class myClass {
constructor() {
}
setConfig(obj) {
this.config= obj
}
getConfig() {
return this.config;
}
}
And in another file I want to get this data 在另一个文件中,我想获取此数据
import m from 'mithril';
import { myClass } from '../model';
export function Accounts() {
console.log('accon')
let asad=new myClass();
console.log(asad.getConfig())
}
the problem is it create new instance of class how can I share instance of this class so that is works 问题是它创建了类的新实例,我如何共享该类的实例,所以可行
I have done this by making singleton class in es6 using following code. 我通过使用以下代码在es6中创建单例类来完成此操作。
export class globals {
constructor() {
if (!globals.instance) {
globals.instance = this
}
return globals.instance;
}
setConfig(obj) {
this.config= obj
}
getConfig() {
return this.config;
}
}
and then I'm calling get config method working perfectly 然后我调用get config方法可以正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.