标签: angular-router

如何使用带有路由器插座的 sidenav 和带有路由器插座的单独登录?

我正在尝试实现一个带有登录页面的应用程序,然后当用户登录时,会有一个导航栏、工具栏和 sidenav 带有router-outlet.

基本上,在app.component.html我有一个定义:

<div *ngIf="isAuthenticated">
    <app-custom-navbar></app-custom-navbar>
    <app-custom-toolbar></app-custom-toolbar>
    <app-custom-sidenav></app-custom-sidenav>
</div>

<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

我有这个路由:

const routes: Routes = [
    { path: '', canActivate: [AuthenticationGuard], component: HomeComponent },
    { path: 'login', component: LoginComponent },
    { path: 'data', canActivate: [AuthenticationGuard], component: DataComponent },
    { path: 'projects', canActivate: [AuthenticationGuard], component: ProjectsComponent },
];
Run Code Online (Sandbox Code Playgroud)

因此,这很有效,如果用户未通过身份验证,他将被重定向到/login.

问题是,当他通过身份验证时,sidenav 组件占据了整个空间,然后其他组件就看不到了。

基本上这是问题:

<mat-sidenav-container class="sidenav-container">
  <mat-sidenav>

    <!-- sidenav template... -->

  </mat-sidenav>
  <mat-sidenav-content>

   <!-- how can I put my content here and use routing at the …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular-material angular-material2 angular angular-router

9
推荐指数
1
解决办法
2万
查看次数

Angular:forRoot/forChild方法用法?

我很惊讶没有确切的答案:

Root/forChild的方法是什么?

例如在RouterModule中:

Router.forRoot(routes)
Run Code Online (Sandbox Code Playgroud)

angular-ngmodel angular angular-router

9
推荐指数
2
解决办法
3536
查看次数

createUrlTree 忽略“useHash: true” RouterModule 配置

在具有“useHash: true”路由器模块配置的 Angular 7 单页应用程序中,我需要生成要在新选项卡中打开的资源的 url。

对于应用程序窗口中的路由,以下代码按预期工作:

this.router.navigate(['foo', 1]);

这意味着,它生成的 url 如下: http://localhost:4200/#/foo/1

虽然使用以下方法时: const url = this.router.createUrlTree(['foo', 1]).toString();

url 是“/foo/1”——没有“#”,所以...

window.open(url, '_blank'); 结果与无效的网址:

http://localhost:4200/foo/1

我发现的唯一解决方案(黑客)非常残酷:

window.open('#' + url, '_blank');

RouterModule.forRoot(routes, {
  useHash: true,
  onSameUrlNavigation: 'reload',
}

Run Code Online (Sandbox Code Playgroud)
showItem(item: Item) {
  // THIS WORKS AS EXPECTED
  this.router.navigate(['item', item.id]);
}

showItemInNewTab(item: Item) {
  const url  = this.router.createUrlTree(['item', item.id]).toString();

  // THIS WORKS BUT ITS A HACK :/
  window.open('#' + url, '_blank');
}

Run Code Online (Sandbox Code Playgroud)

angular angular-router angular7

9
推荐指数
1
解决办法
510
查看次数

加载同一模块的多个路径 - 延迟加载

我在一个模块中有多个组件。我想根据路由路径显示组件。对于http://localhost:4200/account我想显示帐户组件。对于http://localhost:4200/setting我想显示设置组件 ..etc

app.routing.module.ts

{
    path: 'account',
    loadChildren: './modules/settings/settings.module#SettingsModule',
},
{
    path: 'settings',
    loadChildren:'./modules/settings/settings.module#SettingsModule', 
},
Run Code Online (Sandbox Code Playgroud)

settings.routing.module.ts

export const routes: Routes = [
    {
        path: 'account',
        component: accountComponent
    },
    {
        path: 'account/edit',
        component: accountEditComponent
    },
    {
        path: 'settings',
        component: settingsComponent
    },
    {
        path: 'settings/edit',
        component: settingsEditComponent
    }
];
Run Code Online (Sandbox Code Playgroud)

我在 settings.routing.module.ts 中做了哪些更改以显示这些组件。

angular angular-router

9
推荐指数
1
解决办法
3252
查看次数

当路线改变角度时如何运行函数?

我想根据 Angular 中的当前 url 更改某个标题。所以 component.ts 看起来像这样:

import {Router} from '@angular/router';
//code
public name: string
constructor(
    public router: Router
  ) {}
getName()
{
if(this.router.url === "/some_list")
{this.name = "List"}
else if(this.router.url === "/detail")
{this.name = "Detail"}
}

Run Code Online (Sandbox Code Playgroud)

进而

<a>{{this.name}}</a>
Run Code Online (Sandbox Code Playgroud)

现有的答案,例如如何检测 Angular 中的路线变化?无法告诉我在路由更改后如何做某事(更改 = true 或 false)。那么如何在 url 更改时运行此函数,以便标题根据当前视图?

routing angular angular-router

9
推荐指数
2
解决办法
1万
查看次数

获取组件中的路由参数

我正在研究 Angular-6 项目。我的应用程序有很多子路由。其中之一如下:

  const routes: Routes = [
  {
    path: 'innovation-track/:innovation-track-id', component: InnovationTrackComponent,
    children: [
      {
        path: 'sprint', component: ScrumBoardComponent
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

我想从我的ScrumBoardComponent 中的URL获取创新轨道 ID值。

我知道我可以通过执行以下操作来做到这一点:

 constructor(private _route: ActivatedRoute) { }

 //somewhere in method     
   this._route.snapshot.parent.paramMap
Run Code Online (Sandbox Code Playgroud)

但是,我想在任何组件中获取创新轨道 ID值,无论它是子组件还是父组件都没有关系。我的意思是我不想做以下事情:

this._route.snapshot.parent.paramMap
Run Code Online (Sandbox Code Playgroud)

相反,我想做以下事情:

 this._route.params.<any parameter name>
Run Code Online (Sandbox Code Playgroud)

typescript angular-routing angular angular-router

8
推荐指数
1
解决办法
1万
查看次数

angular 8:为什么组件没有加载?

所以我用angular-cli. 我在src目录中有以下结构。

. ??? app ?   ??? app-routing.module.ts ?   ??? app.component.css ?   ??? app.component.html ?   ??? app.component.spec.ts ?   ??? app.component.ts ?   ??? app.module.ts ?   ??? notifications ?   ??? notifications.component.css ?   ??? notifications.component.html ?   ??? notifications.component.spec.ts ?   ??? notifications.component.ts ??? assets ??? environments ?   ??? environment.prod.ts ?   ??? environment.ts ??? favicon.ico ??? index.html ??? main.ts ??? polyfills.ts ??? protocol.js ??? styles.css ??? test.ts

路线有以下。

RouterModule.forRoot([
        { path: '', component: AppComponent },
        { path: 'notifications', component: NotificationsComponent}
    ]) …
Run Code Online (Sandbox Code Playgroud)

angular angular-router angular8

8
推荐指数
1
解决办法
1万
查看次数

列出带有延迟加载模块的 Angular 9 应用程序的所有路由

我想在我的应用程序中有一条路线,我可以在其中列出应用程序中的所有现有路线。我在 Angular 9 中使用 Ivy 并使用动态导入进行延迟加载。

该应用程序有一些延迟加载的模块。该路由将在根模块中声明。但是此时router.config只包含“根”路由,而懒惰的路由有一个 loadChildren()` 方法,可以动态导入模块。

我尝试过这样的事情:角度 8 中的延迟加载模块,但无法使其适用于路线。路由器实例似乎没有更新。

查看路由器的源代码,我已经成功加载了lazy模块并使用configLoaderload方法获取了子路由

(<any> this.router).configLoader.load(this.injector, route).subscribe({
  next: (moduleConf) => {
    children.push(...moduleConf.routes.map((childRoute) => childRoute.path));
  }
})
Run Code Online (Sandbox Code Playgroud)

它运行良好,但我使用了 的configLoader属性,Router它是一个私有属性。所以这对未来来说不是一个好主意:)

我制作了一个堆栈闪电战,您可以在其中查看/测试代码。该组件还有一个loadConfOld()方法,该方法尝试使用moduleFactory.

我怎样才能以适当和持久的方式做我想做的事?预先感谢您的帮助。

lazy-loading angular-module angular angular-router

8
推荐指数
1
解决办法
929
查看次数

HTML 基本 href 和 A​​ngular APP_BASE_HREF 有什么区别?

我正在开发一个 Angular 应用程序,我面临着设置 HTML 基本 href 值和/或 APP_BASE_HREF 值。

它们之间有什么区别和联系呢?

html javascript angular angular-router

8
推荐指数
1
解决办法
7973
查看次数

Angular 路由器出口 - 错误:无法匹配任何路由。网址段

我提前道歉,因为我看过几篇关于此的帖子,但尽管经过多次尝试,我仍无法解决我的问题。

我使用的是 Angular 13.3.9

我尝试使用此处所述的出口路由器:https ://angular.io/api/router/RouterOutlet

我的目标是根据html代码中不同地方的路由加载组件。

这是尝试过的最简单的代码:

路由器

const routes: Routes = [
  { path: 'challenge/pwd', component: ChallengePwd, outlet: 'challenge-pwd' },
  { path: 'identifier', component: Identifier, outlet: 'identifier' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

应用程序组件.html

<main>
    <section>
        <logo-loader></logo-loader>
        <section class="presentation">
            <router-outlet name="identifier"></router-outlet>
            <router-outlet name="challenge-pwd"></router-outlet>
        </section>
    </section>
</main>
Run Code Online (Sandbox Code Playgroud)

当我尝试访问 url http://localhost:4201/identifier 时

我收到错误:Error: Cannot match any routes. URL Segment: 'identifier'

我还尝试将出口路由封装在父组件中,如下所示:

const routes: Routes = [
  {
    path: 'oauth',
    component: MainPage, …
Run Code Online (Sandbox Code Playgroud)

router-outlet angular angular-router

8
推荐指数
1
解决办法
4万
查看次数