Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 获取Angular5中的localstorage对象函数时出错_Typescript_Local Storage_Angular5_Xmpp - Fatal编程技术网

Typescript 获取Angular5中的localstorage对象函数时出错

Typescript 获取Angular5中的localstorage对象函数时出错,typescript,local-storage,angular5,xmpp,Typescript,Local Storage,Angular5,Xmpp,在使用CircularJSON.stringify时,我无法获取localstorage对象函数。 这是我的密码 //set localstorage data localStorage.setItem("client", CircularJSON.stringify(client)); // client is connection object //get localstorage data let client = CircularJSON.parse(localStorage.g

在使用
CircularJSON.stringify
时,我无法获取localstorage对象函数。 这是我的密码

//set localstorage data  
localStorage.setItem("client", CircularJSON.stringify(client)); // client is connection object 

//get localstorage data
let client = CircularJSON.parse(localStorage.getItem('client'));
获取客户机对象时,只有变量显示,而不是所有函数

同时设置本地存储

同时获取本地存储

JSON.stringify()将函数转换为NULL或从对象中删除键值对。一种方法是序列化整个函数并保存它。稍后检索时,反序列化函数


不过,我不建议在LocalStorage中保存函数。

我有一个客户端对象xmpp.createClient(stanza.io),我如何保存此对象以与Angular5中的所有组件共享它,或者您有任何解决方案来共享对象函数和应用程序的整个生命周期创建一个全局服务,注入根组件,使其成为一个单实例,以便在整个应用程序中只使用一个实例。在您需要的任何组件中使用此服务。在我的服务类中,我有getter和setter get client():object{return this.\u client;}set client(value:object){this.\u client=value;}通过这种方式,我可以从任何地方访问具体对象,但是,当我刷新页面时,客户端对象将变得未定义,它将释放其所有值,因此需要将其存储在某个位置,因此我使用LocalStorage来存储此对象,或者有另一种方法可以持久化此对象。我查看了文档,有用于会话的API。页面刷新检查会话是否仍处于活动状态,如果会话处于活动状态,则获取会话实例,否则将用户重定向回登录页面。