【发布时间】:2019-12-26 11:49:22
【问题描述】:
我是 Angular 的新手,我用两个组件创建了非常简单的应用程序,我只想从一个组件导航到另一个组件。
但第二个组件在相同(第一个下方)组件中打开。我怎样才能打开 2nd comp。在新页面中?
这是我的代码
<td> <a routerLink="devicelist">Privacy Policy</a> </td>
<div>
<router-outlet></router-outlet>
</div>
app-route.module.ts 文件
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProductsComponent } from './products/products.component';
import { DeviceListComponent } from './device-list/device-list.component';
const routes: Routes = [
{ path: 'devicelist', component: DeviceListComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule { }
编辑:1
app.component.html
<router-outlet></router-outlet>
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProductsComponent } from './products/products.component';
import { DeviceListComponent } from './device-list/device-list.component';
const routes: Routes = [
{
path: '',
component: ProductsComponent,
},
{ path: 'devicelist', component: DeviceListComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule { }
Comp1 文件
<div>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table >
<tr >
<th>Product Name </th>
<th>Compliance</th>
<th>Failed</th>
<th>Inprogress</th>
<th>Reprocess</th>
</tr>
<tr>
<td>Connected Courrier</td>
<td> <a routerLink="devicelist">Privacy Policy</a> </td>
<td>Failed</td>
<td>Inprogress</td>
<td>Reprocess </td>
</tr>
<tr>
<td>Admin App</td>
<td>20</td>
<td>Failed</td>
<td>Inprogress</td>
<td>Reprocess </td>
</tr>
</table>
</div>
Comp2 文件
<div>
<p>device-list works!</p>
</div>
【问题讨论】:
-
路由器插座是插入当前路由组件的位置。它在您的第一个组件中。因此,当您导航到您的唯一路线时,第二个组件将插入第一个组件中,靠近路由器插座。如果您希望第一个组件消失,那么路由器出口应该在您的应用组件中,并且您的其他两个组件都应该是路由的组件。
-
您的浏览器控制台有错误吗?在空路由上设置 pathMatch: full 有影响吗?
标签: angular routerlink