angular2新路由器不会在页面加载时同时加载2个组件(头和内容)

angular2 new router will not load 2 components (head & content) simultaneously on page load

本文关键字:加载 组件 2个 angular2 新路 路由器      更新时间:2023-09-26

My Angular2 beta.0测试应用程序有一个HeaderFooter应用程序,它会出现在页面加载中。页眉和页脚之间是一个内容部分。第一个问题:我希望在使用HeaderFooter组件的同一页面加载事件上使用Home组件加载内容部分。经过大量的研究和研究,它仍然没有显示两个组件都处于负载状态。第二个问题:我可以通过点击标题中的链接来显示测试主页内容,它会转发到另一个测试页面。它不能可靠地向后路由,浏览器的后退和前进按钮不能可靠地路由,即2步可以,3步不可以。以下是相关代码:

index.html

<div>
  <residence-app><h1>Loading . . .</h1></residence-app>
</div>

boot.ts-这可能会引导所有组件,而不需要单独的组件引导。当我在每个组件中放入引导程序时,我会收到关于选择器的控制台错误,但它们对我来说没有任何意义

import { bootstrap }    from 'angular2/platform/browser';
import { HeaderFooter } from './app';
import { RouteConfig, Router, ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy } from 'angular2/router';
bootstrap( HeaderFooter, [ ROUTER_PROVIDERS ] );

应用

//various imports
@Component({
  selector: 'residence-app',
  templateUrl: "angular2-oPost/src/components/navigation/headerFooter.html",
  styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
  directives: [ ROUTER_DIRECTIVES, Home, PostApartment4Rent ]
@RouteConfig( [
  new Route({ path: "/home", name: "Home", component: Home, useAsDefault: true }),
  new Route({ path: "/postApartment4Rent ",  name: "PostApartment4Rent", component: PostApartment4Rent })
] )
export class HeaderFooter { }

headerFooter.html

<header>
<!-- several divs-->
<a [routerLink]="['../Home']">Home</a>
<a [routerLink]="['../PostApartment4Rent']">Rent Apartment in hF</a>
</header> 
<div class="partialPage">
  <router-outlet></router-outlet>
</div>
<footer>
  <!-- several divs-->
</footer>

主页.ts

@Component({
  selector : "home",
  styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
  template: `
  <main>
    <h1>Home page map</h1>
  </main>
  `,
  directives: [ RouterLink ]
})
export class Home { }

postApartment4Rent.ts-与home.ts相同,除了@Component中的选择器、h1文本和类语句

selector: "postApartment4Rent",
export class PostApartment4Rent { }

通过阅读和尝试更多教程,我将boot.ts中的最后一行从:更改为

bootstrap( HeaderFooter, [ ROUTER_PROVIDERS ] );

至:

bootstrap( HeaderFooter, [ ROUTER_PROVIDERS, provide(LocationStrategy, 
    {useClass: HashLocationStrategy}) ] );

我还必须从angular2/core导入提供。

在页面加载时,"主页"组件现在同时出现在页眉和页脚组件的页眉和页脚之间。页面之间的路由基本上是可以的,但使用浏览器后退按钮是不可靠的。我现在就让它过去,因为我读到浏览器的历史API目前是不稳定的。