其实加载外部html数据有点复杂。
尽管您可以在主index.html文件中定义的场景之间进行切换,使其可见= true/false,但可以通过多种方式实现从其他位置加载内容,我将介绍其中的两个,即使许多框架启用加载外部HTML数据:
JS/JQuery的
我已经成功地使一个小故障,其中的场景元素是从scene1.html文件加载:https://glitch.com/edit/#!/jungle-aardvark?path=index.html:14:34
我试图找到最优化的way,但只这一个使用jQuery的设法给了我一些结果。
var xhr= new XMLHttpRequest();
xhr.open('GET', 'scene1.html', true);
xhr.onreadystatechange= function() {
if (this.readyState!==4) return;
if (this.status!==200) return; // or whatever error handling you want
document.querySelector('body').innerHTML= this.responseText;
};
xhr.send();
框架,如ANGULAR
Angular使您切换整个HTML + CSS +打字稿 '包',并通过routing交换它们,或者只是切换可见HTML元素。 我在plunker上做了一些简单的实验。如果你看看,有负责在app.ts路由有两件事情:
您需要导入的路由和您的场景:
import { aScene } from './a-scene.comp.ts';
import { aScenePhoto } from './a-scene.photo.comp.ts';
import { mainPanel } from './mainpanel.comp.ts';
import {Routes,RouterModule} from'@angular/router';
然后,您可以定义路径
const routes: Routes = [
{path: 'simple',component:aScene},
{path: 'photo',component:aScenePhoto},
{path: '',component:mainPanel},
{path: 'photo/:photoAsset',component:aScenePhoto},
,并宣布路由,以及场景:
imports: [ BrowserModule,
FormsModule,
routing],
declarations: [ App,
aScene,
aScenePhoto,
mainPanel,
],
检查出来我的链接plunke河以上元素负责交换场景。
最后,用几句话来描述它并不容易,如果你想使用外部框架,那么需要对框架进行一些研究。
我对react.js很不熟悉,所以我不会进入它,但我想有一些交换代码块的规则,以实现相同。