Angular 2 条路线在通过浏览器导航时不起作用 URL
Angular 2 Routes not working while navigating through browser URL
我在 Angular 项目的路由方面遇到了一些问题,主要是子路由的第三层。在应用程序上导航时,路线运行良好。 (routerLink
) 并且在通过浏览器 URL 访问 URL 时出现问题 URL.
我的Angular版本是2.4.0
我正在使用 ng serve
在开发服务器上进行测试。
对于具有给定结构的项目,
.
├── photos
├── posts
├── users
│ ├── detail
│ │ ├── address
│ │ ├── family
│ │ ├── information
│ │ └── phones
│ ├── friends
│ └── profile
└── videos
下面是代码,
// user routes
export const userRoutes : Routes = [
{
path: 'detail',
component: DetailComponent
},
{
path: 'friends',
component: FriendsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
//app routes
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'photos',
component: PhotosComponent
},
{
path: 'posts',
component: PostsComponent
},
{
path: 'users',
component: UserListComponent
},
{
path: 'user/:username',
component: UserComponent,
children: userRoutes
},
{
path: 'videos',
component: VideosComponent
}
]
export const AppRoutes = RouterModule.forRoot(appRoutes);
export const appRoutingProviders: any[] = [];
并注册为,
@NgModule({
declarations: [
// declarations
],
imports: [
AppRoutes
],
providers: [
appRoutingProviders
],
bootstrap: [AppComponent]
})
export class AppModule { }
因此该应用程序与 RouterLink 配合得很好
[routerLink]="['/user', username, 'detail']
但是当我浏览浏览器时 <host>/user/myuser/detail
它引发异常
Uncaught (in promise): Error: Cannot find primary outlet to load 'DetailComponent'
怎么了?谢谢。
注意:我已经设置 <router-outlet>
并且在 routerLink 上运行良好,当在浏览器中浏览完整 url 时出现问题。
在 html routerLink="/user" 中使用此代码,请在 routing.ts 中提及您的路径,当您单击此路由器时 link 它将跳转到相关 link
你设置了吗<base href="/">?
你的后端是什么?如果用户首先导航到您应用程序子页面的完整 URL(例如 www.mysite.com/users/john-doe)并且您的后端没有 return angular 2 bootstrapped 页面(index.html 和 bootstrap 脚本),然后 angular 路由器永远不会启动,尽管我认为这不是你的问题,因为您从 angular 收到错误消息。
对于非静态文件的请求,我将我的 .net 核心后端设置为始终 return index.html,因此它始终正确加载应用程序并使用 angular 路由。
我假设当您从 user/:username/
导航到 /user/:username/detail
时,您需要 UserComponent
隐藏他模板上的所有内容并显示 DetailComponent
因此,要实现这一点,您需要一个能够加载其子组件的组件:
parent.component.html
<router-outlet></router-outlet>
routes.ts
现在让我们设置路由以便user/:username/
加载父组件
// user routes
export const userRoutes : Routes = [
{
path: '',
component: UserComponent // the '' path will load UserComponent
},
{
path: 'detail',
component: DetailComponent
},
{
path: 'friends',
component: FriendsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
//app routes
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'photos',
component: PhotosComponent
},
{
path: 'posts',
component: PostsComponent
},
{
path: 'users',
component: UserListComponent
},
{
path: 'user/:username',
component: ParentComponent, //This component will load its children
children: userRoutes
},
{
path: 'videos',
component: VideosComponent
}
]
export const AppRoutes = RouterModule.forRoot(appRoutes);
export const appRoutingProviders: any[] = [];
所以,这里发生的是,当您导航到 user/:username
时,ParentComponent
被加载,然后 UserComponent
被加载到父组件的出口(因为 ''
路径对应至 user/:username
)
这是一个工作示例:Github
访问user/myuser
时会显示:用户在工作!
user/myuser/details
将显示:详细信息有效!如下所示(不使用路由器链接)
在标签中使用 <base href="/">
。
注意 <base href="/">
它应该在 head 标签内的顶部。
<head>
<base href="/">
other css and jquery ...
</head>
像
这样的路由
const appRoutes: Routes = [
{
path: 'Admin',
component: AdminComponent,
children: [
{
path: 'CandidateInfo',
component: CandidateInfoComponent
},
{
path: 'RptCandidateInfo',
component: RptCandidateInfoComponent
},
{
path: 'RptCandidateInfoDT',
component: RptCandidateDTInfoComponent
}
]
},
];
它工作正常。
我在 Angular 项目的路由方面遇到了一些问题,主要是子路由的第三层。在应用程序上导航时,路线运行良好。 (routerLink
) 并且在通过浏览器 URL 访问 URL 时出现问题 URL.
我的Angular版本是2.4.0
我正在使用 ng serve
在开发服务器上进行测试。
对于具有给定结构的项目,
.
├── photos
├── posts
├── users
│ ├── detail
│ │ ├── address
│ │ ├── family
│ │ ├── information
│ │ └── phones
│ ├── friends
│ └── profile
└── videos
下面是代码,
// user routes
export const userRoutes : Routes = [
{
path: 'detail',
component: DetailComponent
},
{
path: 'friends',
component: FriendsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
//app routes
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'photos',
component: PhotosComponent
},
{
path: 'posts',
component: PostsComponent
},
{
path: 'users',
component: UserListComponent
},
{
path: 'user/:username',
component: UserComponent,
children: userRoutes
},
{
path: 'videos',
component: VideosComponent
}
]
export const AppRoutes = RouterModule.forRoot(appRoutes);
export const appRoutingProviders: any[] = [];
并注册为,
@NgModule({
declarations: [
// declarations
],
imports: [
AppRoutes
],
providers: [
appRoutingProviders
],
bootstrap: [AppComponent]
})
export class AppModule { }
因此该应用程序与 RouterLink 配合得很好
[routerLink]="['/user', username, 'detail']
但是当我浏览浏览器时 <host>/user/myuser/detail
它引发异常
Uncaught (in promise): Error: Cannot find primary outlet to load 'DetailComponent'
怎么了?谢谢。
注意:我已经设置 <router-outlet>
并且在 routerLink 上运行良好,当在浏览器中浏览完整 url 时出现问题。
在 html routerLink="/user" 中使用此代码,请在 routing.ts 中提及您的路径,当您单击此路由器时 link 它将跳转到相关 link
你设置了吗<base href="/">?
你的后端是什么?如果用户首先导航到您应用程序子页面的完整 URL(例如 www.mysite.com/users/john-doe)并且您的后端没有 return angular 2 bootstrapped 页面(index.html 和 bootstrap 脚本),然后 angular 路由器永远不会启动,尽管我认为这不是你的问题,因为您从 angular 收到错误消息。
对于非静态文件的请求,我将我的 .net 核心后端设置为始终 return index.html,因此它始终正确加载应用程序并使用 angular 路由。
我假设当您从 user/:username/
导航到 /user/:username/detail
时,您需要 UserComponent
隐藏他模板上的所有内容并显示 DetailComponent
因此,要实现这一点,您需要一个能够加载其子组件的组件:
parent.component.html
<router-outlet></router-outlet>
routes.ts
现在让我们设置路由以便user/:username/
加载父组件
// user routes
export const userRoutes : Routes = [
{
path: '',
component: UserComponent // the '' path will load UserComponent
},
{
path: 'detail',
component: DetailComponent
},
{
path: 'friends',
component: FriendsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
//app routes
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'photos',
component: PhotosComponent
},
{
path: 'posts',
component: PostsComponent
},
{
path: 'users',
component: UserListComponent
},
{
path: 'user/:username',
component: ParentComponent, //This component will load its children
children: userRoutes
},
{
path: 'videos',
component: VideosComponent
}
]
export const AppRoutes = RouterModule.forRoot(appRoutes);
export const appRoutingProviders: any[] = [];
所以,这里发生的是,当您导航到 user/:username
时,ParentComponent
被加载,然后 UserComponent
被加载到父组件的出口(因为 ''
路径对应至 user/:username
)
这是一个工作示例:Github
访问user/myuser
时会显示:用户在工作!
user/myuser/details
将显示:详细信息有效!如下所示(不使用路由器链接)
在标签中使用 <base href="/">
。
注意 <base href="/">
它应该在 head 标签内的顶部。
<head>
<base href="/">
other css and jquery ...
</head>
像
这样的路由const appRoutes: Routes = [
{
path: 'Admin',
component: AdminComponent,
children: [
{
path: 'CandidateInfo',
component: CandidateInfoComponent
},
{
path: 'RptCandidateInfo',
component: RptCandidateInfoComponent
},
{
path: 'RptCandidateInfoDT',
component: RptCandidateDTInfoComponent
}
]
},
];
它工作正常。