我的 Angular2 beta.0 测试应用程序有一个在页面加载时出现的 HeaderFooter 应用程序。在页眉和页脚之间是内容部分。第一个问题:我希望内容部分在与 HeaderFooter 组件相同的页面加载事件中与 Home 组件一起加载。经过大量研究和折腾,它仍然没有显示加载时的两个组件。第二个问题:我可以通过单击标题中的链接来显示测试主页内容,并将其路由到另一个测试页面。它不能可靠地向后路由,浏览器的后退和前进按钮也不能可靠地路由,即 2 步可以,3 步不行。以下是相关代码:
索引.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 ] );
应用程序.ts
//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 文本和 class 语句
selector: "postApartment4Rent",
export class PostApartment4Rent { }