简体   繁体   English

Angular2 rc5>组件未加载

[英]Angular2 rc5> component does not get loaded

I have recently moved to Angular2 RC5 from RC4. 我最近从RC4搬到了Angular2 RC5。 Since then I have encountered couple of problems. 从那时起,我遇到了几个问题。 I'm not sure whether these problems are my because of fault or transitional. 由于过错或过渡,我不确定这些问题是不是我的问题。 My app component looks like this : 我的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();
  }
}

routing : 路由:

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 module : 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 {}

My first problem is if I do not add 1.HeaderComponent, 2.FooterComponent in bootstrap of app.module, none of them(1.HeaderComponent, 2.FooterComponent) get loaded when root route is active (localhost:3000), but the SearchBoxComponent. 我的第一个问题是如果我没有在app.module的bootstrap中添加1.HeaderComponent,2.FooterComponent,当根路由处于活动状态时,它们(1.HeaderComponent,2.FooterComponent)都不会被加载(localhost:3000),但是SearchBoxComponent。 I am kind of confused, since I did not see adding multiple components in bootstrap this in the official document. 我有点困惑,因为我没有在官方文档中看到在bootstrap中添加多个组件。

My second problem is almost same as the first one. 我的第二个问题几乎与第一个问题相同。 If I embed a component (seachBoxConmponent) in another component like the following code, seachBoxConmponent component does not get loaded but the other parts. 如果我在另一个组件中嵌入一个组件(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>
`
})

I was wondering , can anyone please help me, I have been working on this problem for last couple of days, still I am stuck here. 我想知道,有谁可以帮助我,我最近几天一直在研究这个问题,但我仍然被困在这里。

Make sure the module that declares a component exports it. 确保声明组件的模块将其导出 Otherwise this component will not be visible to other components trying to use it. 否则,尝试使用它的其他组件将无法看到此组件。

I would suggest creating separate modules for discrete concerns such as search, signup, chat, etc. and following the pattern below to share their components. 我建议为搜索,注册,聊天等离散问题创建单独的模块,并按照下面的模式分享他们的组件。

I've noticed that Angular2 fails silently when a component being used is not in scope. 我注意到当使用的组件不在范围内时,Angular2会无声地失败。 You'll add a component to the template and it'll just not render, no error. 您将向模板添加一个组件,它不会渲染,也不会出错。 Before RC5 it usually meant you did not specify the desired component in the directives:[] array. 在RC5之前,它通常意味着您没有在指令中指定所需的组件:[]数组。 With RC5, it probably means you're not exporting the component from the module that declares it and/or importing it in the module that wants to use it. 使用RC5,这可能意味着您不是从声明它的模块中导出组件和/或在想要使用它的模块中导入它。

FooModule declares and exports FooComponent, exposing it for use by other components (potentially in other modules): FooModule声明并导出FooComponent,公开它以供其他组件使用(可能在其他模块中):

@NgModule({
    declarations: [FooComponent],
    imports: [BrowserModule, FormsModule],
    exports: [FooComponent]

})
export class FooModule {}

FooComponent: FooComponent:

@Component({
    selector: 'foo',
    template: `FOO`
})
export class FooComponent {}

BarModule imports FooModule, gaining access to the components it exposes (namely FooComponent): BarModule导入FooModule,可以访问它公开的组件(即FooComponent):

@NgModule({
    declarations: [BarComponent],
    imports: [FooModule, FormsModule],
    exports: [BarComponent]

})
export class BarModule {}

BarComponent can access FooComponent and use it in the template: BarComponent可以访问FooComponent并在模板中使用它:

@Component({
    selector: 'bar',
    template: `<foo></foo>BAR`
})
export class BarComponent {}

I'm having all sorts of trouble migrating to RC5 too, but only AppComponent should be in your bootstrap. 我在迁移到RC5时遇到了各种麻烦,但只有AppComponent应该在你的bootstrap中。 If you only get to a component via the router, then that component should not be in declarations. 如果您只通过路由器访问组件,那么该组件不应该在声明中。 But that component should use export default class ComponentName. 但该组件应使用导出默认类ComponentName。

the way I've been attacking this is commenting everything out and adding components and services one at a time. 我一直在攻击它的方式是评论所有内容并一次添加一个组件和服务。

Thank you guys for helping me. 谢谢你们帮助我。 Finally, I solved the problem. 最后,我解决了这个问题。 I was actually including ROUTER_DIRECTIVES in HeaderComponent and FooterComponenet. 我实际上在HeaderComponent和FooterComponenet中包含了ROUTER_DIRECTIVES。 These are the places where conflicts occur. 这些是发生冲突的地方。 It is because "RouterModule" implicitly provides ROUTER_DIRECTIVES. 这是因为“RouterModule”隐式提供了ROUTER_DIRECTIVES。 So, we do not have to include the directive again in any component. 因此,我们不必在任何组件中再次包含该指令。 I removed this directive and that fixes my problem after almost a week later. 我删除了这个指令,并在几乎一周之后修复了我的问题。 I also had to change the old fashioned routerLike style according to the docs. 我也不得不根据文档改变老式的routerLike风格。

@Alex Sartan : Thanks very much for explaining the details of the problem. @Alex Sartan:非常感谢您解释问题的细节。 I fix the problem without creating separate modules for discrete concerns. 我解决了这个问题而没有为离散问题创建单独的模块。 But this is better to keep all the concerns separate. 但是,最好将所有问题分开。 I appreciate your help. 我感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM