通过服务将参数传递给ngOnInit [英] Passing param to ngOnInit through service

查看:125
本文介绍了通过服务将参数传递给ngOnInit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习有关角度2的问题,我对我认为是否可能的情况有疑问.

I just started learning about angular 2 and I am having questions on if the scenario I think is possible.

我有一个微服务模型,其中有与每个微服务关联的角度应用程序.

I have a micro service model where in I have angular apps associated to each micro service.

  • 微服务1和ng app 1-用于处理交易 用户
  • 服务2和ng应用2-用于计算用户的所有适用税金
  • Microservice 1 & ng app 1 - is for handling the transactions of the user
  • service 2 and ng app 2 - is to calculate all applicable taxes for the user

如果我进入应用程序1,输入交易明细,然后单击继续按钮,我应该能够将税收计算所需的值与用户ID一起传递给全部"?

If I land on app 1, enter the details of my transaction and click a continue button, I should be able to pass "All" the values that are required for tax calculation along with the user Id?

通过URL传递应该可以,但是我有用户ID,transactionID,交易金额等,以确保安全.

Passing it through URL should work but i have user ID, transactionID, transaction amount etc to be bit secure.

我能否通过ngOnInit()或某些生命周期事件传递它,以便ng app 2获取这些详细信息,并根据传递的init参数将页面的税额加载到页面上?帮助我:)

Would I be able to pass it through ngOnInit() or through some life cycle event so that ng app 2 gets those details and the page loads with the tax values based on the init params passed? Help me on this :)

推荐答案

好吧,您似乎拥有的是Microfrontends.就像每个微服务都是为非常具体的实体设计的一样,每个微前端也都是为非常具体的实体设计的.这就是您似乎拥有的.

Well, what you seem to have is Microfrontends. Just like each microservice is designed for a very specific entity, each micro frontend is designed for a very specific entity. And that's what you seem to have.

在微前端之间共享数据的一种非常常见的方法是定义自定义事件.

A very common approach of sharing data between micro frontends is by defining custom events.

微型前端(A)可以发出如下事件:

A micro frontend(A) can emit an event like this:

// this is attached to the button in micro-frontend A
btn.onclick = () => {
  const event = new Event("a__click");
  window.dispatchEvent(event);
};

另一个微型前端(B)可以监听该事件并做出相应的反应:

Another micro frontend(B) can listen to that event and react accordingly:

// fire this when the micro-frontend initializes
window.addEventListener("a__click", () => this.onUpdateCount());

// actual method to update the count
onUpdateCount(amt = 1) {
  this.state.count += amt;
  const countEl = this.shadowRoot.getElementById("b__count");
  countEl.innerHTML = this.state.count;
}

这是 令人启发的文章 在Medium上由本杰明·约翰逊(Benjamin Johnson)组成,您可能想阅读以了解更多信息.

Here's an amazingly enlightening article on Medium by a guy named Benjamin Johnson that you might want to read to know more about it.

话虽这么说,由于这些是DOM事件,因此有人仍然可以以某种方式截获它们.在这种情况下,您可以实现一个自定义的微服务,该微服务可以返回特定的敏感信息,然后对此进行处理.

That being said, since these are DOM Events, someone could still intercept them somehow. In those cases, you could have a custom microservice implemented that could return a particular sensitive information and then do the needful with that.

这篇关于通过服务将参数传递给ngOnInit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆