javascript - 如何在两个不同的 Angular 应用程序之间传递数据

标签 javascript html angular

这是我的要求

我有两个 Angular Web 应用程序 App1 和 App2。在 App1 中,我正在创建一个 token 对象(它也可以是一个字符串)。

我想通过将 App1 中的 url 替换为 App2 的 URL 来打开(不应打开新窗口,它应该在同一地址栏上打开 App2 url)

这里我已经尝试了下面的方法,目前还没有用

“App1”中的这段代码通过按下我触发的按钮 App2

App1中编写的代码

goToNextPage(){



var domain = 'http://localhost:1338/';
// //create popup window

var myPopup = window.open(domain,"_self"); 



// //periodical message sender
 setInterval(function(){
    var message = 'current time: ' + (new Date().getTime());
    console.log('blog.local:  sending message:  ' + message);
   myPopup.postMessage(message,domain);
    //send the message and target URI

 },2000);


 window.addEventListener('message',function(event) {


    if(event.origin !== 'http://localhost:1337/') return;
    console.log('received response:  ',event.data);
 },false);


  }

在 App2 中,我使用以下代码来检索数据。

constructor(public cookieService:CookieService){




//     //respond to events
 window.addEventListener('message',function(event) {
  debugger

//   //
     if(event.origin == 'http://localhost:1337/') return;
  console.log('message received:  ' + event.data,event);
  alert('message received:  ' + JSON.stringify(event.data))
//  //event.source.postMessage('holla back youngin!',event.origin);
 },false);
  }

我得到的 event.dataundefined

知道有什么办法可以解决这个问题

最佳答案

您可以让服务器端共享 api 或使用查询字符串在 url 上传递数据。如果两个应用程序在同一个域上运行,cookie、 session 存储和本地存储等东西就可以工作。

关于javascript - 如何在两个不同的 Angular 应用程序之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59273771/

相关文章:

javascript - 如何在 JavaScript 中将 px 转换为 vw?

javascript - 将带有换行符的文本从 Javascript 设置为 HTML

html - margin-top 未按预期运行

node.js - 如果用户经过身份验证,则使用 E​​xpress 服务 Angular 应用程序

javascript - Angular 没有返回预期值

javascript - computeDistanceBetween 似乎计算不正确

javascript - magicScroll 仅预制动画第一次向下页面

html - float 父级仅由内容 float div 扩展

javascript - 努力根据特定列的值隐藏行(理论上应该很容易......)

Angular2 *ngIf ="afunctioncall()"导致函数被调用 9 次