Angular 4路由怪异的行为:除非在页面刷新上,否则不要渲染儿童模块



我一直在寻找这种怪异行为的解决方案。

  • Angular 4控制台或JavaScript控制台中没有错误
    • 使用角CLI(Angular 4)

我使用角路由器的行为极为奇怪。我正在尝试从父苹果模块加载具有自己的模块的子路由。该路线在/产品

在/产品模块(products.module.ts):我有3个路线:

  1. /products/概述
  2. /products/intro [重定向到/products]
  3. /products/product/:产品

产品模块应该在Global AppModule的Navbar下具有自己的Navbar,该Navbar在products.component.component.html组件模板中的路由器范围上方。

主要问题:当我加载页面(不使用Angular Routerlink)时

  • 两个Navbars都是可见的
  • 加载的页面(EX/INTRO)可见NAVBAR,但子组件(Introcomponent)是 not 可见
  • 但是,当我导航到/产品模块路由中的任何其他子程序时(ex/概述或/product/:product):它的呈现不错。因此,在整页上加载的任何路线都不可见。

最奇怪的部分:当我将页面(刷新)加载到不是/products(ex/home)的路由上时,然后使用主纳维尔键导航到/产品:ul>

  • Navbar是看不到 ????????
  • 可见的子组件(ex/intro组件)
  • 这在过去几天里一直引起了我的大量地狱。

    现在,一些代码:

    app.module.ts [main AppModule]

        @NgModule({
        declarations: [
            AppComponent,
            HomeComponent,
            LoginComponent,
            SignupComponent,
            NotFoundComponent,
            LogoutComponent,
            ErrorComponent,
            AboutComponent,
            ApiComponent,
            StatusComponent,
            SupportComponent,
            CustomersComponent,
            JobsComponent,
            TosComponent
        ],
        imports: [
            BrowserModule,
            HttpModule,
            FormsModule,
            SharedModule,
            //ProductsModule,
            AppRoutingModule
        ],
        providers: [
            UserService,
            AuthGuard,
            ErrorService,
            NavbarService,
            MailerService,
            IOService,
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    

    shared.module.ts - 这具有我的ViewAppComponent(这只是带有全局NAVBAR的路由器出口)和其他全局组件(例如应在所有路线上显示的Navbar和页脚)

    @NgModule({
      imports: [
        CommonModule,
          RouterModule
      ],
      declarations: [ViewAppComponent, FooterComponent, NavbarComponent],
      exports: [CommonModule, ViewAppComponent, FooterComponent, NavbarComponent]
    })
    export class SharedModule { }
    

    app-routing.module.ts

    const routes: Routes = [
        {
            path: '',
            component: ViewAppComponent,
            children: [
                {path: 'products', loadChildren: () => ProductsModule}, // where im trying to load my products module from the main module
                {path: 'home', component: HomeComponent},
                {path: 'about', component: AboutComponent},
                {path: 'api', component: ApiComponent},
                {path: 'status', component: StatusComponent},
                {path: 'support', component: SupportComponent},
                {path: 'customers', component: CustomersComponent},
                {path: 'jobs', component: JobsComponent},
                {path: 'tos', component: TosComponent},
                {path: 'signup', component: SignupComponent, canActivate: [AuthGuard]},
                {path: 'logout', component: LogoutComponent, canActivate: [AuthGuard]},
                {path: 'login', component: LoginComponent},
                {path: 'error', component: ErrorComponent, canActivate: [ErrorService]},
                {path: "404", component: NotFoundComponent},
                {path: '', redirectTo: '/home', pathMatch: 'full'},
            ]
        },
        {path: "admin", loadChildren: "./admin/admin.module#AdminModule"},
        {path: '**', redirectTo: '/404'}
    ];
    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule {}
    

    products.module.ts [主要产品模块在其中我试图加载子流量]

    @NgModule({
        imports: [
            CommonModule,
            FormsModule,
            SharedModule,
            RouterModule,
            ProductsRoutingModule
        ],
        declarations: [ProductsComponent, OverviewComponent, IntroComponent, NavigationComponent, ProductComponent],
        exports: [
            ProductsComponent, OverviewComponent, IntroComponent, NavigationComponent, ProductComponent, RouterModule, ProductsRoutingModule
        ]
    })
    export class ProductsModule {
    }
    

    products-routing.module.ts [产品suppoule的路由器]

    export const productRoutes: Routes = [
        {
            path: '',
            component: ProductsComponent,
            children: [
                {
                    path: 'overview',
                    component: OverviewComponent,
                },
                {
                    path: 'intro',
                    component: IntroComponent
                },
                {
                    path: 'product/:product',
                    component: ProductComponent
                },
                {
                    path: '',
                    redirectTo: '/products/intro',
                    pathMatch: 'full'
                },
                {
                    path: '**',
                    redirectTo: '/404',
                    pathMatch: 'full'
                }
            ]
        }
    ];
    @NgModule({
        imports: [RouterModule.forChild(productRoutes)],
        exports: [RouterModule]
    })
    export class ProductsRoutingModule {}
    

    products.component.html [产品中用于子组件的产品的模板]

    <h1>Products</h1>
    <br/>
    <app-product-navigation></app-product-navigation>
    <br/>
    <router-outlet></router-outlet>
    

    navigation.component.html [app-product-Navigation组件模板]

    <nav class="navbar navbar-info" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#products-navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="collapse navbar-collapse" id="products-navbar-collapse">
          <ul class="nav navbar-nav">
            <li routerLinkActive="active"><a routerLink="/products/intro">Intro</a></li>
            <li routerLinkActive="active"><a routerLink="/products/overview">Overview</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <b class="caret"></b></a>
              <ul class="dropdown-menu">
                <li routerLinkActive="active"><a routerLink="/products/product/reflex">Reflex</a></li>
                <li routerLinkActive="active"><a routerLink="/products/product/override">Project Override</a></li>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <br/>
    

    我还将提供我的软件包。版本的依赖项,我正在使用NG服务进行测试。

      "dependencies": {
        "@agm/core": "1.0.0-beta.0",
        "@angular/animations": "4.3.1",
        "@angular/cdk": "2.0.0-beta.10",
        "@angular/common": "4.3.1",
        "@angular/compiler": "4.3.1",
        "@angular/core": "4.3.1",
        "@angular/forms": "4.3.1",
        "@angular/http": "4.3.1",
        "@angular/material": "2.0.0-beta.10",
        "@angular/platform-browser": "4.3.1",
        "@angular/platform-browser-dynamic": "4.3.1",
        "@angular/platform-server": "4.3.1",
        "@angular/router": "4.3.1",
        "angular2-material-datepicker": "0.5.0",
        "animate.css": "3.5.2",
        "arrive": "2.3.1",
        "bootstrap": "3.3.5",
        "bootstrap-material-design": "0.5.10",
        "bootstrap-notify": "3.1.3",
        "bootstrap-select": "1.12.2",
        "bootstrap-tagsinput": "0.7.1",
        "chartist": "0.9.4",
        "chartist-plugin-zoom": "0.4.0",
        "core-js": "2.4.1",
        "datatables": "1.10.12",
        "datatables.net-bs": "1.10.12",
        "datatables.net-responsive": "2.1.1",
        "eonasdan-bootstrap-datetimepicker": "4.17.47",
        "fullcalendar": "3.4.0",
        "googleapis": "19.0.0",
        "jasny-bootstrap": "3.1.3",
        "jquery": "1.12.4",
        "moment": "2.18.1",
        "moment-timezone": "0.4.0",
        "ng2-nouislider": "1.6.1",
        "ng2-select": "1.2.0",
        "ngx-chips": "1.4.6",
        "nouislider": "9.2.0",
        "rxjs": "^5.4.2",
        "twitter-bootstrap-wizard": "1.2.0",
        "typescript": "2.3.4",
        "validate": "3.0.1",
        "web-animations-js": "2.2.2",
        "zone.js": "0.8.4"
      },
      "devDependencies": {
        "@angular/cli": "1.4.2",
        "@angular/compiler-cli": "4.3.1",
        "@types/bootstrap": "3.3.32",
        "@types/chartist": "0.9.34",
        "@types/jasmine": "2.5.38",
        "@types/jquery": "1.10.31",
        "@types/node": "6.0.73",
        "codelyzer": "2.0.0",
        "jasmine-core": "2.5.2",
        "jasmine-spec-reporter": "3.2.0",
        "karma": "1.4.1",
        "karma-chrome-launcher": "2.0.0",
        "karma-cli": "1.0.1",
        "karma-coverage-istanbul-reporter": "0.2.0",
        "karma-jasmine": "1.1.0",
        "karma-jasmine-html-reporter": "0.2.2",
        "node-sass": "^4.5.3",
        "protractor": "5.1.0",
        "ts-node": "2.0.0",
        "tslint": "4.5.0",
        "typescript": "2.3.4"
      }
    }
    

    这是我在Stackoverflow上的第一个"寻求帮助"帖子,所以让我知道我是否可以改进。

    我试图研究各种形式和教程,并引用了Angular Doc进行路由

    编辑:添加一些图片以进行更多澄清

    主页:http://prntscr.com/gskex5

    products [/products/intro](导航到使用Routerlink):http://prntscr.com/gskf2h(丢失了产品Navbar)在刷新中,纳维托变得可见,但介绍组件消失了:http://prntscr.com/gskfaq

    但是,当我单击产品导航中的另一个组件时,它可以正常工作:http://prntscr.com/gskfhf

    如果我刷新,则组件消失了,剩下NAV:http://prntscr.com/gskfna现在,无论出于何种原因,如果我回到介绍,它都可以正常工作:http://prntscr.com/gskfra

    基本上,当页面最初加载时加载的任何路由,都有问题[不可见组件]。如果最初加载的路线不是/产品;产品Navbar不可见。

    在这种相当奇怪的情况下,我能够通过使用Angular-cli生成新的Angular 4应用程序来解决这种奇怪的行为: ng new new

    我猜这与我正在运行的Angular版本有关。

    对于我的新项目, @angular 版本设置为**^4.0.0*。

    在旧项目中,版本设置为4.3.1

    我猜想这与v4.3.1的错误文件中的一个更改有关:

    • 路由器:candeactivate守卫应从下到顶部(1AC78BF), 关闭#15657
    • 路由器:应在配置时导航到同一URL 更改(4340BEA),关闭#15535
    • 路由器:应运行解析器 同时(EC89F37),关闭#14279
    • 路由器:终端 自定义匹配器(5D275E9)中的路线

    tl; dr

    使用NG4的最新稳定版本

    最新更新