【发布时间】:2019-09-30 22:28:58
【问题描述】:
当我加载第一个组件并尝试将其链接到其他组件时遇到问题,第一个初始组件往往与其他组件保持一致。基本上,合并的组件。
我现在正在尝试编写第一个代码,所以当它加载时,它会立即路由到主页。
如果我先加载主页,那么每当我从主页转到另一个页面时,主页都会停留。合并主页面和我去的页面。
我觉得 Angular 应该自动执行此操作以阻止这种情况发生。
代码
第一个组件。
<!-- Pesdo Example -->
<a onload="anotherComponent">
或者如果我可以让 routerLink 以某种方式立即链接到另一个而不点击。我知道 onLoad 用于脚本。
第二
<div class="background">
<div class="logo">
<img src="/images/logo.png" alt="Foo Logo">
<br><br>
<h1 class="title">Foo App</h1>
</div>
<div class="options">
<h2>Foo</h2>
<a><button class="button">Foo 1</button></a>
<a><button class="button">Foo 2</button></a>
</div>
</div>
app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MainMenuComponent } from './main-menu/main-menu.component';
import { NavComponent } from './nav/nav.component';
@NgModule({
declarations: [
AppComponent,
MainMenuComponent,
NavComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Routes
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainMenuComponent } from './main-menu/main-menu.component';
const routes: Routes = [
{ path: "main", component: MainMenuComponent },
];
app.component.html
<app-nav></app-nav>
<section>
<router-outlet></router-outlet>
</section>
Thanks!
EDIT:
I can now load up the first component but now routerLink does not work to route to other components.
<section>
<router-outlet></router-outlet>
</section>
const routes: Routes = [
{ path: "", pathMatch: "full", redirectTo: "home"},
{ path: "Contact", pathMatch: "full", redirectTo: "contact" },
{ path: "Settings", pathMatch: "full", redirectTo: "settings" },
{ path: 'contact', component: MainFormComponent },
{ path: 'home', component: ListOfPeopleComponent },
{ path: 'settings', component: EditAPersonComponent },
];
Neither the names from BOTH PATHS work for router link.
<a routerLink="Settings"></a> <a routerLink="settings"></a> both do not work.
EDIT: My console outputs this.
[![enter image description here][1]][1]
Please copy and paste the link, I do now know why SO is displaying all this text as code. Thanks.
[1]: https://i.stack.imgur.com/TNOT5.png
【问题讨论】:
-
我们能看到你的 app.module.ts + 你的路由是在哪里定义的吗?
-
我已回滚您的编辑。不适合在标题中添加 (SOLVED) 或在您的问题中编辑解决方案。如果您发现下面的答案给了您答案,那么表明您的问题已解决的正确方法是通过选中该答案分数下方的标记来接受答案。如果您找到了其他解决方案并想分享,请在下方为此目的提供的空白处写下答案 - 请参阅Can I answer my own question?。
标签: angular routing components