我最近从RC4搬到了Angular2 RC5。从那时起,我遇到了几个问题。由于过错或过渡,我不确定这些问题是不是我的问题。我的app组件如下所示:
import {Component, OnInit} from "@angular/core";
import {SetLocationService} from "./auth/set-location.service";
@Component({
selector : "my-app",
template: `
<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>
`
})
export class AppComponent implements OnInit{
constructor(
private _setLocationService : SetLocationService
){}
ngOnInit() {
this._setLocationService.setLocation();
}
}
路由:
import {Routes, RouterModule} from '@angular/router';
import {SearchBoxComponent} from "./search/searchBox.component";
import {SearchResultComponent} from "./search/search-result.component";
import {LoginComponent} from "./auth/login.component";
import {SignupComponent} from "./auth/signup.component";
import {LogoutComponent} from "./auth/logout.component";
import {RecoverPasswordComponent} from "./auth/recover-password.component";
import {ProfileComponent} from "./auth/profile.component"
import {AccountComponent} from "./auth/account.component"
const appRoutes: Routes = [
{path : '', component: SearchBoxComponent},
{path : 'login', component: LoginComponent},
{path : 'signup', component: SignupComponent},
{path : 'logout', component: LogoutComponent},
{path : 'profile', component: ProfileComponent},
{path : 'account', component: AccountComponent},
{path : 'user/:uname', component: SearchBoxComponent},
{path : 'recover-password', component: RecoverPasswordComponent},
{path : 'search/:params', component: SearchResultComponent},
{path : '**', component : SearchBoxComponent}
];
export const routing = RouterModule.forRoot(appRoutes);
app模块:
// @Modules -> Modules
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
import {NgModule} from '@angular/core';
// import {RouterModule} from '@angular/router';
// @Routes -> routes
import {routing} from "./app.routes";
// @Components - > Components
import {AccountComponent} from "./auth/account.component";
import {AppComponent} from './app.component';
import {ChatBoxComponent} from "./chat/chat-box.component";
import {ChatBoxDirective} from "./chat/chat-box.directive";
import {FooterComponent} from "./layout/footer.component";
import {HeaderComponent} from "./layout/header.component";
import {LoginComponent} from "./auth/login.component";
import {LogoutComponent} from "./auth/logout.component";
import {ProfileComponent} from "./auth/profile.component";
import {RecoverPasswordComponent} from "./auth/recover-password.component";
import {SearchBoxComponent} from "./search/searchBox.component";
import {SearchResultComponent} from "./search/search-result.component";
import {SignupComponent} from "./auth/signup.component";
// @providers - > services
import {AuthService} from "./auth/auth.service";
import {SetLocationService} from "./auth/set-location.service";
import {SearchService} from "./search/search.service";
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
declarations: [
AccountComponent,
AppComponent,
ChatBoxComponent,
ChatBoxDirective,
FooterComponent,
HeaderComponent,
LoginComponent,
LogoutComponent,
ProfileComponent,
RecoverPasswordComponent,
SearchBoxComponent,
SearchResultComponent,
SignupComponent
],
providers : [
AuthService,
SetLocationService,
SearchService,
{provide: LocationStrategy, useClass: HashLocationStrategy}
],
bootstrap: [
AppComponent,
HeaderComponent,
FooterComponent
]
})
export class AppModule {}
我的第一个问题是如果我在app.module的bootstrap中没有添加1.HeaderComponent,2.FooterComponent,当根路由处于活动状态时,它们都不会被加载(1.HeaderComponent,2.FooterComponent)(localhost:3000) ,但是SearchBoxComponent。我有点困惑,因为我没有在官方文档中看到在bootstrap中添加多个组件。
我的第二个问题与第一个问题几乎相同。如果我在另一个组件中嵌入一个组件(seachBoxConmponent),如下面的代码,seachBoxConmponent组件不会加载,但其他部分。
@Component({
selector: "search-result",
template : `
<seachBox></searchBox>
<div class="tag_list">
<p *ngFor = "let tag of result.obj.tags" class = "tag-li" >
<a [routerLink] = "['/search', tag]" (click) = "onSearchCliked($event, tag)"> {{tag}} </a>
</p>
</div>
`
})
我很想知道,任何人都可以帮助我,过去几天我一直在研究这个问题,但我仍然被困在这里。
答案 0 :(得分:7)
确保声明组件的模块导出它。否则,试图使用它的其他组件将无法看到此组件。
我建议为搜索,注册,聊天等离散问题创建单独的模块,并按照下面的模式分享他们的组件。
我注意到当使用的组件不在范围内时,Angular2会无声地失败。您将向模板添加一个组件,它不会渲染,也不会出错。在RC5之前,它通常意味着您没有在指令中指定所需的组件:[]数组。使用RC5,可能意味着您没有从声明它的模块中导出组件和/或在想要使用它的模块中导入它。
FooModule声明并导出FooComponent,公开它供其他组件使用(可能在其他模块中):
@NgModule({
declarations: [FooComponent],
imports: [BrowserModule, FormsModule],
exports: [FooComponent]
})
export class FooModule {}
FooComponent:
@Component({
selector: 'foo',
template: `FOO`
})
export class FooComponent {}
BarModule导入FooModule,获得对它公开的组件的访问权限(即FooComponent):
@NgModule({
declarations: [BarComponent],
imports: [FooModule, FormsModule],
exports: [BarComponent]
})
export class BarModule {}
BarComponent可以访问FooComponent并在模板中使用它:
@Component({
selector: 'bar',
template: `<foo></foo>BAR`
})
export class BarComponent {}
答案 1 :(得分:1)
我在迁移到RC5时遇到了各种麻烦,但只有AppComponent应该在你的bootstrap中。如果您只通过路由器访问组件,那么该组件不应该在声明中。但该组件应使用export default class ComponentName。
我一直在攻击它的方式是评论所有内容并一次添加一个组件和服务。
答案 2 :(得分:0)
谢谢你们帮助我。最后,我解决了这个问题。我实际上在HeaderComponent和FooterComponenet中包含了ROUTER_DIRECTIVES。这些是发生冲突的地方。这是因为“RouterModule”隐式提供了ROUTER_DIRECTIVES。因此,我们不必在任何组件中再次包含该指令。我删除了这个指令,并在几乎一周之后修复了我的问题。我也不得不根据文档改变老式的routerLike风格。
@Alex Sartan:非常感谢您解释问题的详细信息。我解决了这个问题而没有为离散问题创建单独的模块。但是,最好将所有问题分开。感谢您的帮助。