在另一个 Angular 2 应用程序中嵌入一个 Angular 2 应用程序
Embed an Angular 2 app inside another Angular 2 app
我的任务是实施一个基于 angular 2 的应用程序
提供页眉、导航栏和页脚。所有非常标准的东西。
但是在导航栏之间,其他angular2个应用都可以
显示哪个应用程序取决于用户在导航中的选择
酒吧。
嵌入式 Angular 2 应用程序通过它们自己的 URL 访问。
导航栏是使用来自
REST API 以允许轻松配置。
听起来这可以使用 IFRAME 实现,但是有吗
使用 IFRAME 的任何替代方法,以及使用
浏览器的前后按钮、键盘快捷键等。
我可以动态调整 IFRAME 大小以适应嵌入的大小吗?
angular 应用?即使嵌入式应用程序的大小发生变化?
嵌入 angular 2 个应用程序或任何其他相关内容,可以使用 IFrame 实现:
import {Component, OnInit, ElementRef, ViewChild} from "@angular/core";
import {NavigationService} from "../navigation.service";
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'framed-content',
template: `
<div class="row content application-box">
<div class="col-xs-12 col-sm-12 col-md-12">
<iframe #iframe style="width: 100%;height: 100vh;position: relative;"
[src]="selectedContent | safe"
frameborder="0"
allowfullscreen></iframe>
</div>
</div>
`
})
export class FramedContentComponent implements OnInit {
@ViewChild('iframe') iframe: ElementRef;
private selectedContent = 'content/home.html';
constructor(private route: ActivatedRoute, private navigationService: NavigationService) {
}
/**
* Registers a callback for retrieving the target based on the selected content
*/
ngOnInit() {
this.route.params.map(params => params['selectedItem']).subscribe((selectedItem) => {
this.selectedContent = this.navigationService.getNavigationItem(selectedItem).target;
})
}
}
剩下的唯一问题是调整 IFrame 的大小以适应内容。
这是在容器应用程序的 de main index.html 中使用一段普通的 javascript 解决的:
<script language="JavaScript1.5">
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (~event.origin.indexOf('http://localhost')) {
// The data sent with postMessage is stored in event.data
console.log(event.data);
resize(event.data);
} else {
// The data hasn't been sent from your site!
return;
}
});
function resize(frameHeight) {
console.log('Parent resize');
frame=document.getElementById('loketFrame');
console.log(frame);
frame.height=frameHeight;
}
</script>
每次嵌入式应用程序更改其大小时,都需要post向容器发送消息以更新 iframe 的高度:
ngOnInit() {
setTimeout(() => {
var height = document.documentElement.scrollHeight;
console.log('Resizing to ' + height);
parent.postMessage(height, '*');
}, 0);
}
我的任务是实施一个基于 angular 2 的应用程序 提供页眉、导航栏和页脚。所有非常标准的东西。 但是在导航栏之间,其他angular2个应用都可以 显示哪个应用程序取决于用户在导航中的选择 酒吧。 嵌入式 Angular 2 应用程序通过它们自己的 URL 访问。 导航栏是使用来自 REST API 以允许轻松配置。
听起来这可以使用 IFRAME 实现,但是有吗 使用 IFRAME 的任何替代方法,以及使用 浏览器的前后按钮、键盘快捷键等。
我可以动态调整 IFRAME 大小以适应嵌入的大小吗? angular 应用?即使嵌入式应用程序的大小发生变化?
嵌入 angular 2 个应用程序或任何其他相关内容,可以使用 IFrame 实现:
import {Component, OnInit, ElementRef, ViewChild} from "@angular/core";
import {NavigationService} from "../navigation.service";
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'framed-content',
template: `
<div class="row content application-box">
<div class="col-xs-12 col-sm-12 col-md-12">
<iframe #iframe style="width: 100%;height: 100vh;position: relative;"
[src]="selectedContent | safe"
frameborder="0"
allowfullscreen></iframe>
</div>
</div>
`
})
export class FramedContentComponent implements OnInit {
@ViewChild('iframe') iframe: ElementRef;
private selectedContent = 'content/home.html';
constructor(private route: ActivatedRoute, private navigationService: NavigationService) {
}
/**
* Registers a callback for retrieving the target based on the selected content
*/
ngOnInit() {
this.route.params.map(params => params['selectedItem']).subscribe((selectedItem) => {
this.selectedContent = this.navigationService.getNavigationItem(selectedItem).target;
})
}
}
剩下的唯一问题是调整 IFrame 的大小以适应内容。 这是在容器应用程序的 de main index.html 中使用一段普通的 javascript 解决的:
<script language="JavaScript1.5">
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (~event.origin.indexOf('http://localhost')) {
// The data sent with postMessage is stored in event.data
console.log(event.data);
resize(event.data);
} else {
// The data hasn't been sent from your site!
return;
}
});
function resize(frameHeight) {
console.log('Parent resize');
frame=document.getElementById('loketFrame');
console.log(frame);
frame.height=frameHeight;
}
</script>
每次嵌入式应用程序更改其大小时,都需要post向容器发送消息以更新 iframe 的高度:
ngOnInit() {
setTimeout(() => {
var height = document.documentElement.scrollHeight;
console.log('Resizing to ' + height);
parent.postMessage(height, '*');
}, 0);
}