子组件显示在另一个子组件下方。

huangapple go评论51阅读模式
英文:

Child component is displayed under another child component

问题

Child #1 组件:

selector: 'app-home'

Child #2 组件:

selector: 'app-plans'

Child #1 HTML:

<p>child 1 here</p>

Child #2 HTML:

<p>Child 2 here</p>

app.component.html:

<app-home></app-home>

app-routing.module.ts:

const routes: Routes = [
    { path: 'plans', component: PlansComponent },
    { path: 'home', component: HomeComponent }
]

在浏览器中访问 localhost:4200/plans 显示:

child 1 here
child 2 here

如果我将 app.component.html 更改为:

<router-outlet></router-outlet>

localhost:4200/plans 显示:

child 2 here

为什么在 app.component.html 中使用 'app-home' 选择器有问题?为什么在 localhost:4200/plans 中显示 "child 1 here"?(我期望只显示 "child 2 here"。)

显示了我期望的结果,但我不明白为什么 不像我期望的那样工作。

英文:

Child #1 Component:
selector: 'app-home'

child #2 component:
selector: 'app-plans'

child #1 html:

    &lt;p&gt;child 1 here&lt;/p&gt;

child #2 html:

    &lt;p&gt;Child 2 here&lt;/p&gt;

app.component.html:

&lt;app-home&gt;&lt;/app-home&gt;

app-routing.module.ts:

    const routes: Routes = [
    {path: &#39;plans&#39;, component: PlansComponent},
    {home: &#39;home&#39;, component: HomeComponent} 
    ]

in browser localhost:4200/plans displays:
child 1 here
child 2 here

if I change app.component.html to:

    &lt;router-outlet&gt;&lt;/router-outlet&gt;

localhost:4200/plans displays:
child 2 here

What's wrong with using 'app-home' selector in app.component.html? why is it showing "child 1 here" in localhost:4200/plans? (i am expecting "child 2 here" only.)

<router-outlet></router-outlet> shows results I expected, but I don't understand why <app-home></app-home> doesn't work as I expected.

答案1

得分: 0

如果你想使用路由策略,需要在你的主组件中插入RouterOutlet指令,就像你这样做的&lt;router-outlet&gt;&lt;/router-outlet&gt;一样。RouterOutlet指令是一个占位符,Angular会根据活动路由动态填充适当的内容。

所以,如果你有一个活动路由,比如localhost:4200/plans,并且想要看到在路由定义中配置的组件PlansComponent,那么你应该在你的应用组件中只使用&lt;router-outlet&gt;&lt;/router-outlet&gt;

在你的应用组件中使用&lt;app-home&gt;&lt;/app-home&gt;只是静态地渲染HomeComponent,与你的路由实现无关。

英文:

If you want to use the router strategy you need to insert the RouterOutlet directive in your main component like you did &lt;router-outlet&gt;&lt;/router-outlet&gt;. The RouterOutlet directive is a placeholder that angular fills dynamically with the appropriate content based on the active route.

So if you have an active route like localhost:4200/plans and want to see only the component configured in the route definition PlansComponent, then you should just use &lt;router-outlet&gt;&lt;/router-outlet&gt; in your app component.

With &lt;app-home&gt;&lt;/app-home&gt; in your app component you are just statically rendering the HomeComponent and nothing to do with your routing implementation.

huangapple
  • 本文由 发表于 2023年6月26日 11:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定