如何订阅会话速度可观察的

发布于 2025-02-03 05:33:00 字数 2190 浏览 1 评论 0原文

在Angular组件(FirstComponent)中,我调用了一个服务,从中我将对象作为响应得到。我将响应存储在浏览器中,以“ sessionstorage”。

sessionStorage.setItem("data", JSON.stringify(response));

现在,在另一个组件(第二组件)中,我获得了“ sessionstorage”,然后将结果从内部组件库中绘制到数据杂志中。

let response = JSON.parse(sessionStorage.getItem("data"));

调用组件库方法时,您可以加载数据级列时,它给我以下错误:

具体行错误:

columnsOptions[5].visible = false

core.js:6210 ERROR TypeError: Cannot read properties of undefined (reading '5')
    at TasksContainerComponent.addView (tasks-container.component.ts:144:21)
    at TasksContainerComponent.getScheduleTasks (tasks-container.component.ts:118:16)
    at SafeSubscriber._next (tasks-container.component.ts:52:12)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183:1)
    at SafeSubscriber.next (Subscriber.js:122:1)
    at Subscriber._next (Subscriber.js:72:1)
    at Subscriber.next (Subscriber.js:49:1)
    at MapSubscriber._next (map.js:35:1)
    at MapSubscriber.next (Subscriber.js:49:1)
    at BehaviorSubject._subscribe (BehaviorSubject.js:14:1)

我从第二个组件中正确地从第一个组件的服务中获得了“ SessionStorage”的响应,但是它给出了我指示的错误。有趣的是,如果我在不使用“ SessionStorage”的情况下称呼第二个组件中的第一个组件的相同服务,我会得到相同的响应,并且一切正常。

就像我必须创建一个可观察到的“ sessionStorage”并订阅以正确加载数据,但是到目前为止,我还没有弄清楚如何做到这一点。

要优化,我只想调用服务一次,因为在这两个组件中,我都需要相同的响应来绘制数据。

这是第二个组件的oninit事件,这是当我使用“ sessionstorage”时显示的错误,“ columnsoptions”常数未定义,但是,如果我不使用“ sessionstorage”,并启动“ columnsoptions”服务呼叫,,它具有我需要的价值,而且工作正常。

目的是用“ sessionstorage”中存储的对象绘制数据杂志,但是如果“ haserrors”参数包含0,则我隐藏了datagrid的一列,以防其“ 0”错误,那是我没有错误而且我必须隐藏最后一个数据杂志列,这是我尝试隐藏该列时完全失败的代码:

const columnsOptions: DatagridColumnOptions[] = this.dg.columns?.map(column => ({
  column: column,
  visible: true,
  level: 0,
}));

if (hasErrors === 0) { 
  columnsOptions[5].visible = false;
}

错误在此行上:

columnsOptions[5].visible = false;

但是它是因为:

columnsOptions = undefined;

我可以做什么?

In an Angular component (FirstComponent), I call a service, from which I get an object as a response.I store the response in the browser in a "sessionStorage".

sessionStorage.setItem("data", JSON.stringify(response));

Now, in another component (SecondComponent), I get the "sessionStorage", and paint the results into a Datagrid from an internal component library.

let response = JSON.parse(sessionStorage.getItem("data"));

When calling a component library method with which you can load the Datagrid columns, it gives me the following error:

Concrete line error:

columnsOptions[5].visible = false

core.js:6210 ERROR TypeError: Cannot read properties of undefined (reading '5')
    at TasksContainerComponent.addView (tasks-container.component.ts:144:21)
    at TasksContainerComponent.getScheduleTasks (tasks-container.component.ts:118:16)
    at SafeSubscriber._next (tasks-container.component.ts:52:12)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183:1)
    at SafeSubscriber.next (Subscriber.js:122:1)
    at Subscriber._next (Subscriber.js:72:1)
    at Subscriber.next (Subscriber.js:49:1)
    at MapSubscriber._next (map.js:35:1)
    at MapSubscriber.next (Subscriber.js:49:1)
    at BehaviorSubject._subscribe (BehaviorSubject.js:14:1)

I get the response from the service of the first component correctly in the second component with "SessionStorage", but it gives me the indicated error. The funny thing is that, if I call the same service of the first component in the second component without using "SessionStorage", I get returns the same response, and everything works perfectly.

It's like I have to create a "sessionStorage" observable and subscribe to load the data correctly, but so far I haven't figured out how to do that.

To optimize, I would like to call the service only once, since in both components I need the same response to paint the data.

This is the second component's OnInit event and this is the error it shows me when I use "SessionStorage", the "columnsOptions" constant is not defined, however if I don't use "SessionStorage" and launch the "columnsOptions" service call, it has the value I need and it works fine.

The objective is to paint the Datagrid with the object stored in "SessionStorage", but I hide a column of the Datagrid in case it has "0" errors, if the "hasErrors" parameter contains 0, it is that I haven't errors and I must hide the last Datagrid column, this is the code that fails exclusively when I try to hide the column:

const columnsOptions: DatagridColumnOptions[] = this.dg.columns?.map(column => ({
  column: column,
  visible: true,
  level: 0,
}));

if (hasErrors === 0) { 
  columnsOptions[5].visible = false;
}

The error is on this line:

columnsOptions[5].visible = false;

But it is because:

columnsOptions = undefined;

What I can do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文