【问题标题】:View doesn't change after clicking on routerLink to child route angular 2单击 routerLink 到子路由 angular 2 后视图不会改变
【发布时间】:2017-12-07 03:49:11
【问题描述】:

嘿,我遇到了这个问题,我似乎无法弄清楚出了什么问题,基本上我有一个 routerLink 去一个子路由,url 更改为我想要的路由,但视图没有更新链接到当前活动的子路由的组件

app.component.ts

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>

html

<div class="wide component">
    <div class="page-wrap">
        <img class="logo" src="../../assets/img/our-work-component-logo.png"><br>
        <h2>Component Elite</h2>
        <h3>Web portal and promotions</h3>

        <p class="short-description">Lorem Ipsum</p>
        <a routerLink="component" class="button orange-button">View More</a>
    </div>
</div>

app.module.ts

import { OurWorkComponent } from './our-work/our-work.component';
import { ComponentComponent } from './our-work-content/component/component.component'

export const ROUTES: Routes = [
  { path: 'our-work', component: OurWorkComponent,
      children: [
         { path: 'component', component: ComponentComponent }
      ]
  }
];

@ngModule({
  declarations: [
     OurWorkComponent,
     ComponentComponent
  ],
  imports: [
     BrowserModule,
     RouterModule.forRoot(ROUTES)
  ],
  providers: [],
  bootstrap: [AppComponent]
})

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    它没有改变的原因是因为childrenOurWorkComponent

    children: [
        { path: 'component', component: ComponentComponent }
    ]
    

    这意味着OurWorkComponent 中应该有&lt;router-outlet&gt;,但我看不到,只需在OurWorkComponent's 中添加html 即可。


    更多详情请阅读:https://angular.io/guide/router#nesting-routes

    【讨论】:

    • 但是我们的工作页面上有内容,所以如果我把 放在页面上,只有组件页面才会显示,而不是我们的工作页面,除非我指定另一个页面对于我们的工作页面,但网址将是我们的工作/我们的工作我怎么不这样做?
    • 然后删除children属性并简单使用path: 'our-work/component',
    • 但我想要子路由,所以我想在 our-work 页面上,然后单击另一个链接,然后在另一个页面上,但 url 显示 ourwork/newpage
    • @A61NN5,只需做一件事,在 stackblitz 上创建一个简单的问题演示,以便我们可以更好地帮助您,或者直接通过我在答案中提供的链接,它将消除您所有的疑问关于如何使用路由。
    • 我需要做的就是拥有一个带有 url /our-work 的页面,其中包含我想要的内容,然后能够单击指向另一个页面的链接并将 url 更改为/我们的工作/新页面
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2017-05-27
    • 2017-11-16
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多