我正在try 创建一个具有全局NAV(在router-outlet 之前的AppComponent中)和子路由的应用程序.这个Web应用程序是建立在多租户模型上的,URL是这样的http://serveraddress/tenantSpace/:tenantUrl/,我无法在我的routerLinks相对路径中获得我想要的结果.我总是得到一个/而不是完整的参数路径.

以下是我try 过的:

因此,每个租户的路由将在/tenantSpace/:tenantUrl/之后开始.所以我在导航中为我的routerLinks设置的内容如下:

<a routerLink="./page1">Page1</a>

我希望它能给我带来:

<a routerLink="/tenantSpace/tenantSpace1/page1" href="/tenantSpace/tenantSpace1/page1">Page1</a>

问题是我总是得到/page1.即使我位于与page1相同级别的页面上,也不会计算相对路径,例如在/tenantSpace/tenantSpace1/page2中.

如果我将一个routerlink放在page1组件中,我会得到一个好的router.url,但是当计算AppComponent中的全局导航时,router.url总是/

因此,我确信我不了解ANGLE中的布线工作方式,也找不到任何有关这方面的资源.如果有人能帮忙,我将不胜感激.

这里是一个堆栈闪电战来说明这个问题(您可以在里面找到我的路由配置):

Sample project

推荐答案

实际上,./布线意味着元件当前路由的相对位置,因为它在app.component中,相对位置是/,所以Angular 很好!

为了解决您的问题,您需要创建一个名为webspaces的组件,其中将包含<router-outlet/>,我们将订阅参数webSpaceName的最新值并将其存储在服务中.就像这样

Webspaces.com.ts

import { Component } from '@angular/core';
import { ActivatedRoute, RouterOutlet, Params } from '@angular/router';
import { WebspaceService } from '../webspace.service';
import { Subscription } from 'rxjs';
@Component({
  selector: 'app-webspaces',
  standalone: true,
  imports: [RouterOutlet],
  templateUrl: './webspaces.component.html',
  styleUrl: './webspaces.component.scss',
})
export class WebspacesComponent {
  subscription: Subscription = new Subscription();
  constructor(
    private activatedRoute: ActivatedRoute,
    private webspaceService: WebspaceService
  ) {
    this.subscription.add(
      this.activatedRoute.params.subscribe((params: Params) => {
        this.webspaceService.webspace = params?.['webSpaceName'] || '';
      })
    );
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

网站空间html

<router-outlet/>

在根组件中,当我们路由时,我们附加参数,然后执行路由,如下所示!

应用程序html

<nav>
  <a [routerLink]="['webspaces', webspace, 'properties']">Properties</a>
</nav>
<router-outlet></router-outlet>

应用程序TS

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterLink, RouterModule, RouterOutlet } from '@angular/router';
import { WebspaceService } from './webspace.service';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet, RouterModule, RouterLink],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  title = 'AppTest';

  get webspace() {
    return this.webspaceService.webspace;
  }

  constructor(private webspaceService: WebspaceService) {}
}

stackblitz->;cd test->;npm i->;npm run start

Angular相关问答推荐

如何在Angular中执行一个操作条件为多个可观测值?

身份验证卫士给了我17个Angular 的任何类型

对子router-outlet 应用主机样式

如何在ANGLING v17中使用鞋带样式组件?

更改动态表单控制Angular 的值

如何使用带有17角的Swiper 11.0.5元素

PathMatch full始终连接到参数化路径,即使完整路径不匹配也是如此

错误TS2531:对象可能为空.论窗体数组控件

如何从 RxJS 重试中排除某些 url

个人修改 Angular CLI - 创建新应用

angular 13 ng 构建库失败(ivy部分编译模式)

不再需要 core-js 了吗?

如何在数据表angular material中显示空消息,如果未找到数据

Angular2 Pipes:输出原始 HTML

在 Angular 2 中打印 Html 模板

将外部 CSS 加载到组件中

Angular Material2 主题 - 如何设置应用程序背景?

Angular单元测试输入值

此类通过 SomeModule -> SomeComponent 对消费者可见,但不会从顶级库入口点导出

Angular 4.3 HttpClient:拦截响应