标签: angular-router

Angular 在 Guard 中获取匹配的 url 路径

我的路由是

{
path:'contacts/:id',
component: contactDetail
}
Run Code Online (Sandbox Code Playgroud)

canActivateChild( route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) { 
  console.log(state.url); 
}
Run Code Online (Sandbox Code Playgroud)

state.url返回/contacts/1我需要的是匹配的url/contact/:id是否有可能获得匹配的url?

angular angular-router-guards angular-router angular-routerlink

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

基于 Observable 的 paramMap 方法的用例

我通常在我的应用程序的路线上使用这种方式。

ngOnInit() {
  let id = this.route.snapshot.paramMap.get('id');

  this.hero$ = this.service.getHero(id);
}
Run Code Online (Sandbox Code Playgroud)

但在上面的文档中我也可以看到基于 Observable 的方法。

ngOnInit() {
  this.hero$ = this.route.paramMap.pipe(
    switchMap((params: ParamMap) =>
      this.service.getHero(params.get('id')))
  );
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我为什么我们需要这种方法吗?医生这样说

使用此技术您只能获得参数映射的初始值。如果路由器有可能重用该组件,请坚持使用可观察的 paramMap 方法。

但我不清楚。你能告诉我为什么我们需要基于 Observable 的方法吗?

rxjs typescript angular angular-router

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

通过在路径前添加“%2f”字符来解决 Angular4 路由问题

对我来说使用嵌套路由的原因是动态处理每个页面的图标和后退按钮。我有抽象路由,因为当我单击后退按钮时,路由参数消失,我放置一个抽象路径来保留idquestionnaire在 url 中键入参数。我的抽象路径是Questionaire

 { path: 'Home', component: HomeComponent,data:{icon:'fa-home'} },
 { path: 'QuestionaireList', component: QuestionaireListComponent,data:{icon:'fa-list-ul',previousState:'/Home'} },
 { 
      path: 'Questionaire/:questionnaireType/:id',
      children:[ 
      { path: 'AddQuestionnaire', component: CreateQuestionnaireComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'} },
      { path: 'UpdateQuestionnaire', component: EditComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
      { path: 'QuestionList', component: QuestionListComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
      { path: 'ImportQuestion',    component: ImportQuestionComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'}  },
      { path: 'MetaContentList', component: MetaContentListComponent,data:{icon:'fa-database',previousState:'/QuestionaireList'} },
      { path: 'ModifyMetaContent/:metaContentId/:title', component: ModifyMetaContentComponent,data:{icon:'fa-database',previousState:'/MetaContentList'}},
    ]}
Run Code Online (Sandbox Code Playgroud)

另外,在questionnaire.list.html我通过 routerlink 链接到每条路径时,下面是我的链接:

[routerLink]="['/Questionaire',item.questionnaireType,item.id,'/UpdateQuestionnaire']"
Run Code Online (Sandbox Code Playgroud)

这是我的链接:

 <a  class="primary small ui icon button" [routerLink]="['/Questionaire',item.questionnaireType,item.id,'/UpdateQuestionnaire']" >
                <i class="edit   icon"></i>
              </a>
Run Code Online (Sandbox Code Playgroud)

我预计当我点击链接时我会路由到这个地址: …

angular2-routing angular angular-router angular4-router

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

角线不渲染子组件,而是渲染父组件

我想创建一个嵌套的路由movies/:id但是,使用我当前的配置,当用户导航到movies/1父组件时,始终会呈现。我需要MovieDetailComponentmovies/1url 上渲染。这是我的配置:

const routes: Routes = [{
    path: '',
    component: HomeView
  },
  {
    path: 'movies',
    component: MoviesView,
    children: [{
      path: ':id',
      component: MovieDetailComponent
    }]
  },
  {
    path: 'not-found',
    component: PageNotFoundComponent,
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'not-found'
  }
];
Run Code Online (Sandbox Code Playgroud)

我尝试过先添加pathMatch: 'full'到父组件,然后添加到子组件,然后再添加。当我添加pathMach: 'full'到父组件时,子URL甚至都不会被点击;当我将pathMatch: 'full'just 添加到子组件时,即使URL为,也只会呈现父组件。/movies/:id为什么会这样?

当我将子路径移动到其自己的路径中时(未嵌套),该组件将正确呈现。

const routes: Routes = [{
    path: '',
    component: HomeView
  },
  {
    path: 'movies',
    component: MoviesView
  },
  {
    path: …
Run Code Online (Sandbox Code Playgroud)

routing angular angular-router

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

ionic 4防止/禁用设备硬件后退按钮

我对ionic 4项目使用了角路由(@ angular / router)以禁用ionic 4中的设备后退按钮prevent-default不起作用,下面是我的代码

app.component.ts

    this.platform.backButton.subscribe(() => {
        if (this.router.url === '/Login') {
          this.util.presentAppExitAlert();
        } else {
          // event.preventDefault();
          console.log("invoing url ", this.router.url);
        }
      });
    });
Run Code Online (Sandbox Code Playgroud)

我无法在此处禁用设备后退按钮的任何帮助

ionic3 angular angular-router ionic4

1
推荐指数
3
解决办法
3887
查看次数

Angular 路由器LinkActiveOptions

我有一些这样的链接

<li routerLinkActive="active" class="nav-item">
<a [routerLink]="['contracts']" 
[queryParams]="{ activeOnly: false }" 
class="nav-link">Contracts</a>
</li>
Run Code Online (Sandbox Code Playgroud)

你在参数中看到我有?activeOnly=false,当我的网址像这样时,使用这种方法一切都可以

Contracts?activeOnly=false 在 html 中添加活动 css 类,用户可以看到它是活动链接

我遇到的问题是,有时 url 可以像这样的 contracts?activeOnly=true&id=1一样更改,然后active css 不再适用。

我需要的是在元素上有活动的 css 类,即使 url 已随查询参数更改。

示例如果 url 是这样的contracts?activeOnly=true&id=1

活动CSS 类也必须保留在LI html 标签上

routerlinkactive angular angular-router

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

如何使用 Angular 路由管理 Angular 中的多篇文章?

我正在尝试使用 Angular 创建一个网站,其中包含许多文章,每当我按下一篇文章时,它就会使用路由转到一个新的 URL。

1

为此,我创建了一个新的文章组件,我的app-routing.module.ts看起来像这样

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'contact', component: ContactComponent },
  { path: 'about', component: AboutComponent },
  { path: 'article1', component: ArticlesComponent },
  { path: 'article2', component: ArticlesComponent },
  { path: 'article3', component: ArticlesComponent },
  { path: 'article4', component: ArticlesComponent },
  { path: 'article5', component: ArticlesComponent },
  { path: 'article6', component: ArticlesComponent },
];
Run Code Online (Sandbox Code Playgroud)

我将所有内容路由到同一个组件,因为我不想为每篇文章创建一个新组件。如何根据用户对一篇文章的点击在此组件上显示不同的数据?

typescript angular angular-router

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

Angular 9 - 导航开始时获取目标网址

我正在使用Angular 9。我需要在导航开始后立即知道哪个将是目标网址。我想做这样的事情:

constructor(router: Router) {
    this.router = router;
}

this.router.events.pipe(
    filter(event => event instanceOf NavigationStart),
    map(event => {
        //get somehow the destination url of this navigation just started
    })
).subscribe()
Run Code Online (Sandbox Code Playgroud)

有没有办法只使用 Angular Router来实现这一点?

url-routing angular-routing angular angular-router angular9

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

如果 'router-outlet' 是一个 Angular 组件,那么验证它是否是这个模块的一部分

我在 angular 11 中遇到了这个错误,但我尝试应用我在网上找到的所有修复程序......

错误 :

错误:src/app/app.component.html:1:1 - 错误 NG8001:'router-outlet' 不是已知元素:如果 'router-outlet' 是一个 Angular 组件,则验证它是否是该模块的一部分

应用程序组件.html

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

app.module.ts

@NgModule({
  imports: [
    AppRoutingModule,
    AppStoreModule,
    BrowserModule,
    BrowserAnimationsModule,
    EffectsModule.forRoot(),
    HttpClientModule,
    AngularSvgIconModule.forRoot(),
    NgbModule,
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: ApolloInterceptor,
      multi: true,
    },
    {
      provide: ErrorHandler,
      useValue: Sentry.createErrorHandler({
        showDialog: true,
      }),
    },
    {
      provide: Sentry.TraceService,
      deps: [Router],
    },
    {
      provide: APP_INITIALIZER,
      useFactory: () => () => {},
      deps: [Sentry.TraceService],
      multi: true,
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

app.routing.ts

import { …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular angular-router

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

子模块的Angular 5路由无法正常工作

我正在使用Angular 5开发Web应用程序.我是Angular with Typescript的新手.现在我正在为我的应用程序模块配置路由.但它不起作用并抛出错误.

这是我的index.html档案

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Web</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <router-outlet></router-outlet>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我在app-routing.module.tsapp文件夹下创建了一个文件.

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { AppComponent } from './app.component';


    const routes: Routes = [ 
        { path: '', loadChildren: './web/web.module#WebModule' },
    ];

    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule {}


Import that routing class in …
Run Code Online (Sandbox Code Playgroud)

typescript angular-routing angular angular-router

0
推荐指数
1
解决办法
2919
查看次数