我一直在玩Angular 2 webpack starter here,它有很多有用的例子 . 我已经删除了一些模块,并添加了我自己的一个模块,其中包含子模块(我正在尝试使用组件进行此操作) .
我的目标是能够正常切换顶层模块(在这种情况下为 Home
和 About
),但在加载我的 Home
模块后,在 Home
模块中交换多个子组件(或模块,如果支持更好)模板 .
我正在尝试在 Home
中使用另一个 <router-outlet>
- 我已尝试在 Home
模块路由定义中命名并使用 outlet
参数,但单击链接以在 Home
中加载当前不起作用 . 代码如下:
这是我目前的应用程序结构(大部分额外的webpack东西都被遗漏了):
app
|-app.component.ts (template contains router-outlet)
|-app.module.ts
|-app.routes.ts
|-about
|-about.component.ts
|-about.component.html
|-home
|-home.module.ts
|-home.component.ts
|-home.component.html (contains router outlet for children)
|-home.routes.ts
|-signup
|-signup.component.ts
|-signup.component.html
|-login
|-login.component.ts
|-login.component.html
这是我的app.component,它与它在启动器repo中的启动几乎相同 - 我刚刚添加了一个链接到我自己的模块并删除了其他模块:
app.component.ts
import {
Component,
OnInit,
ViewEncapsulation
} from '@angular/core';
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
styleUrls: [
'./app.component.css'
],
template: `
<nav>
<a [routerLink]=" ['./home'] " routerLinkActive="active">
Home
</a>
<a [routerLink]=" ['./about'] " routerLinkActive="active">
About
</a>
</nav>
<main>
<router-outlet></router-outlet>
</main>
`
})
export class AppComponent implements OnInit {
constructor(
public appState: AppState
) {}
public ngOnInit() {
console.log('Initial App State', this.appState.state);
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {
NgModule,
ApplicationRef
} from '@angular/core';
import {
removeNgStyles,
createNewHosts,
createInputTransfer
} from '@angularclass/hmr';
import {
RouterModule,
PreloadAllModules
} from '@angular/router';
/*
* Platform and Environment providers/directives/pipes
*/
import { ENV_PROVIDERS } from './environment';
import { ROUTES } from './app.routes';
// App is our top level component
import { AppComponent } from './app.component';
import { APP_RESOLVER_PROVIDERS } from './app.resolver';
import { AppState, InternalStateType } from './app.service';
import { AboutComponent } from './about';
import { HomeModule } from './home';
import { XLargeDirective } from './home/x-large';
// app services needed globally
import { AuthenticationService, UserService, AlertService } from './shared';
import '../styles/styles.scss';
import '../styles/headings.css';
// Application wide providers
const APP_PROVIDERS = [
...APP_RESOLVER_PROVIDERS,
AppState
];
type StoreType = {
state: InternalStateType,
restoreInputValues: () => void,
disposeOldHosts: () => void
};
/**
* `AppModule` is the main entry point into Angular2's bootstraping process
*/
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
AboutComponent,
NoContentComponent,
XLargeDirective
],
imports: [ // import Angular's modules
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(ROUTES, { useHash: true, preloadingStrategy: PreloadAllModules })
],
providers: [ // expose our Services and Providers into Angular's dependency injection
ENV_PROVIDERS,
APP_PROVIDERS,
UserService,
AuthenticationService,
AlertService
]
})
export class AppModule {
constructor(
public appRef: ApplicationRef,
public appState: AppState
) {}
}
app.routes.ts
import { Routes } from '@angular/router';
import { HomeModule } from './home';
import { AboutComponent } from './about';
import { DataResolver } from './app.resolver';
export const ROUTES: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'about', component: AboutComponent },
{ path: 'home', loadChildren: './home#HomeModule' },
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
];
导航到我的Home模块工作正常 - 我认为问题在于它内部的路由 . 这是Home模块,组件和路由 .
home.module.ts
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { routes } from './home.routes';
import { HomeComponent } from './home.component';
import { SignupComponent } from './signup/signup.component';
import { LoginComponent } from './login/login.component';
console.log('`Home` loaded');
@NgModule({
declarations: [
// Components / Directives/ Pipes
HomeComponent,
SignupComponent,
LoginComponent
],
imports: [
CommonModule,
FormsModule,
RouterModule.forChild(routes),
],
})
export class HomeModule {
public static routes = routes;
}
home.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'home',
styleUrls: [ './home.component.css' ],
templateUrl: './home.component.html'
})
export class HomeComponent implements {
}
home.component.html
<h1>Home</h1>
<div>
<h2>Hello from home module</h2>
</div>
<span>
<a [routerLink]=" ['./signup'] ">
Sign Up
</a>
</span>
<span>
<a [routerLink]=" ['./login'] ">
Login
</a>
</span>
<router-outlet name="aux"></router-outlet>
<div>
<h2>Bottom info</h2>
</div>
home.routes.ts
import { HomeComponent } from './home.component';
import { SignupComponent } from './signup/signup.component';
import { LoginComponent } from './login/login.component';
export const routes = [
{ path: '',
component: HomeComponent,
children: [
{
path: 'signup',
component: SignupComponent,
outlet: 'aux'
},
{
path: 'login',
component: LoginComponent,
outlet: 'aux'
}
] }
];
当前的行为是当我单击其中一个链接 Signup
或 Login
时没有任何反应 - 没有错误,但组件也没有在 aux
路由器插座上加载 .
我已经尝试将 Signup
配置为一个模块,但是我没有理解这些额外的配置,并认为将子节点视为组件会帮助我理解基本的所需路由 .
从研究这个问题来看,似乎没有很好的支持多个路由器插座,尤其是使用 router-link
语法,直到RC5左右 . This recent answer对类似的问题表明它现在可以正常工作,但类似的语法对我不起作用 .
将 router-outlets
嵌套是一个问题吗?我应该将 Signup
和 Login
组件定义为模块吗?或者是否有一些其他问题与我定义路由的方式一起,也许是在最高的AppModule级别?
EDIT
我在问题中添加了 app.module.ts
. 另外,在查看我使用的入门项目的基本配置之后,似乎使用子模块可以简化事情 - 具体来说 barrel
模块(可查看的here)有一个 child-barrel
模块,并且它被加载到 barrel.component
模板中的 router-outlet
中 . 如果在同一级别有一个 child-barrel-2
模块,这将是我正在寻找的那种东西 .
我还找到了this question,其中最佳答案表明了这一点
建议使用模块
我是否应该尝试模仿启动器的结构,通过使用模块完成父子关系?
1 回答
为什么在'/' '/'前面使用'.'尝试删除它 . 你的基础href是什么?
EDIT
你为什么不在模板中尝试这样的事情 .
<a [routerLink]="[{outlets: {primary: 'home', aux: 'signup'}}]">signup </a>
以及路由配置中的代码
{path: 'signup', component: signUpComponent, outlet:"aux"}