这是我的要求
我有两个 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.data
是 undefined
知道有什么办法可以解决这个问题
最佳答案
您可以让服务器端共享 api 或使用查询字符串在 url 上传递数据。如果两个应用程序在同一个域上运行,cookie、 session 存储和本地存储等东西就可以工作。
关于javascript - 如何在两个不同的 Angular 应用程序之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59273771/