首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角4路由怪异行为:除非页面刷新,否则不呈现子模块

角4路由怪异行为:除非页面刷新,否则不呈现子模块
EN

Stack Overflow用户
提问于 2017-10-03 02:57:00
回答 1查看 1.2K关注 0票数 2

我一直在寻找解决这个奇怪行为的方法,却没有运气。

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

我对棱角路由器的行为非常奇怪。我正在尝试从父AppModule加载一个子路由,它有自己的模块。路线在/products

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

  1. /产品/概览
  2. /products/intro从/products重定向到
  3. 产品/产品/:产品

产品模块应该在全局AppModule的导航栏下拥有自己的导航条,它位于products.component.html组件模板中路由器出口的上方。

主要问题:当我将页面(不使用角routerLink)加载到/products时,

  • 两个肚脐都是可见的
  • 加载的页面(ex /intro)中的导航条是可见的,但是子组件(IntroComponent)是而不是 visible
  • 但是,当我导航到/products模块路由中的任何其他子路由时(例如/overview或/product/:product):它呈现得很好。因此,在整个页面刷新中加载的任何路由都是不可见的。

是最奇怪的部分:当我在不是/products (例如/home)的路由上加载页面(刷新),然后使用主导航条导航到/products:

  • 肚脐是不可见的?
  • 子组件(ex /intro组件)是可见的。

这在过去的几天里给我带来了很大的麻烦。

现在,一些代码:

app.module.ts main AppModule

代码语言:javascript
复制
    @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 (它只是一个带有全局导航条的路由器插座)和其他全局组件(比如应该在所有路由上显示的导航条和页脚)。

代码语言:javascript
复制
@NgModule({
  imports: [
    CommonModule,
      RouterModule
  ],
  declarations: [ViewAppComponent, FooterComponent, NavbarComponent],
  exports: [CommonModule, ViewAppComponent, FooterComponent, NavbarComponent]
})
export class SharedModule { }

app-routing.module.ts

代码语言:javascript
复制
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主要产品模块,其中im试图加载子路由

代码语言:javascript
复制
@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路由器

代码语言:javascript
复制
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模板,产品中子组件的出口

代码语言:javascript
复制
<h1>Products</h1>
<br/>
<app-product-navigation></app-product-navigation>
<br/>
<router-outlet></router-outlet>

navigation.component.html应用程序-产品-导航组件模板

代码语言:javascript
复制
<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/>

我还将为版本提供我的package.json依赖项,我正在使用ng服务进行测试。

代码语言:javascript
复制
  "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"
  }
}

这是我第一次在堆栈溢出上“寻找帮助”的帖子,所以如果有什么可以改进的,请告诉我。

我尝试过研究各种形式和教程,并参考了用于路由的角度文档

编辑:添加一些图片,以更多地澄清情况

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

Products /产品/介绍http://prntscr.com/gskf2h (缺少产品导航条)在刷新时,导航条变得可见,但是intro组件消失了:http://prntscr.com/gskfaq

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

如果我刷新,组件就消失了,导航器就离开了:http://prntscr.com/gskfna,现在,不管出于什么原因,如果我回到intro,它会正常工作:http://prntscr.com/gskfra

因此,基本上,不管在最初加载页面时加载了什么路由,它的问题组件都是不可见的。如果最初加载的路由不是/products,则产品导航条不可见。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-05 06:43:06

对于这种非常奇怪的情况,我能够通过生成一个具有角cli:ng新的新的角4应用程序来修复这个奇怪的行为。

我猜这和我跑步的角度有关。

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

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

我猜想这与V4.3.1的for中的一个更改有关:

  • 路由器: canDeactivate警卫应该从下到上运行(1ac78bf),关闭#15657
  • 路由器:当配置发生变化(4340bea)时,应该导航到同一个url,关闭#15535
  • 路由器:应该同时运行同一路由的解析器(ec89f37),关闭#14279
  • 路由器:自定义匹配器中的终端路由(5d275e9)

TL;博士

使用ng4的最新稳定版本

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46536562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档