标签: angular2-routing

带有 ngrx 的 Angular 2 路由器 v3 可观察防护

我正在尝试使用 redux(ngrx) 创建一个“身份验证应用程序”,并且我正在尝试在秘密守卫中使用我的应用程序状态。在这里你可以看到我的 github:https : //github.com/tamasfoldi/ngrx-auth/tree/router 这是我的守卫的样子:

@Injectable()
export class SecretGuard implements CanActivate {
  constructor(private store: Store<AppState>, private router: Router) {
  }
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.store.let(getLoginState())
      .map(state$ => state$.isLoggedIn)
  }
}
Run Code Online (Sandbox Code Playgroud)

它返回 isLoggedIn 属性,这应该没问题,因为路由器解析了 promises 和 observable,但是当我导航到秘密部分时路由器阻止了它。以下是我的路线:

export const appRoutes: Routes = [
  {
    path: '',
    redirectTo: 'auth',
    pathMatch: 'full'
  },
  {
    path: 'auth',
    children: [
      { path: '', redirectTo: 'login', pathMatch: 'full' },
      { path: 'login', component: LoginComponent },
      { path: 'register', component: RegisterComponent …
Run Code Online (Sandbox Code Playgroud)

angular2-routing ngrx angular2-router3 angular

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

从 url 获取当前命名的插座

我有一条看起来像这样的路线:

/dashboard/55612291A38AB9CB1C8FE7FB7A166E63/(report//right:logs)
Run Code Online (Sandbox Code Playgroud)

这意味着我有一个 DashboardComponent,在其中我在主要出口内打开了一个 ReportComponent。在我的 DashboardComponent 中,我还有一个已命名的插座(右侧),其中 LogsComponent 处于打开状态。

<dashboard>
    <router-outlet></router-outlet>
    <router-outlet name="right"></router-outlet>
</dashboard>
Run Code Online (Sandbox Code Playgroud)

在我的 ReportComponent 中,我想在正确的插座中打开当前组件的值。有没有一种简单的方法来实现这一目标?

现在似乎可以做

this.route.snapshot._urlSegment.parent.children.right.segments[0].path
Run Code Online (Sandbox Code Playgroud)

有点冗长。谢谢。

router angular2-routing angular2-router angular

5
推荐指数
0
解决办法
490
查看次数

如何导航到延迟加载的模块子路由?

我正在尝试使用在其路由配置文件中声明的子路由延迟加载模块,延迟模块加载成功,但我无法导航到它的子路由。

我正在使用该技术,就好像它是一个急切加载的模块一样。我声明了路径,里面是孩子。请参阅 Plunker 中的“moduleB”路由文件链接按钮正在尝试导航到子路由并加载 CmpC。但它失败了。

这是我的根模块路由声明:

{ path: 'feature-b', loadChildren:'src/featureB/featureB.module#FeatureBModule' ,  pathMatch: 'full'}
Run Code Online (Sandbox Code Playgroud)

这是我的延迟加载模块路由声明。

export const moduleBRoutes: Routes = [

  { path: '', component:CmpC , pathMatch: 'full',children:[
      {path: '' , pathMatch: 'full'},
      { path: 'c', component:CmpB , pathMatch: 'full'}
  ]},

];

export const subRouting: ModuleWithProviders = RouterModule.forChild(moduleBRoutes);
Run Code Online (Sandbox Code Playgroud)

尝试导航到:localhost:999/feature-b 成功

试图导航到:localhost:999/feature-b/c 失败

https://plnkr.co/edit/wFlJoDXRmAlMOswmwWFk?p=preview

angular2-routing angular

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

Angular 2 路由重定向不起作用

我在 app-routing.module 中定义了一个重定向,但不知道为什么它不起作用。如果我使用“”(当前已注释掉的代码)的默认路径而不是重定向,那么它可以正常工作。

@NgModule({
imports: [
    RouterModule.forRoot([
        {
            path: "actions",
            component: ActionsComponent
        },
        //{
        //    path: "",
        //    component: LoginComponent
        //}
        {
            path: "",
            redirectTo: "/login",
            pathMatch: "full"
        }
    ])
],
exports: [
    RouterModule
]
Run Code Online (Sandbox Code Playgroud)

})

有任何想法吗?代码在http://plnkr.co/edit/y970zDKDZRIZ5LQA8v1T?p=preview

angular2-routing angular

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

在 Angular 2 的主/细节示例中订阅子路由更改

我在主/详细视图中遇到路由问题。如果我去的路线/masterInfoComponent应该表现出一些基本信息。如果我去/master/1DetailsComponent应显示为ID 1.在这两种情况下,该项目的一些数据,MasterComponent应该显示无论是选择的ID或没有。

我可以很好地完成这项工作。但问题是我想订阅:idinOnInit中的选定内容,MasterComponent以便我可以在那里突出显示它,如下所示:

this.selectedId = this.route.params.map((params: Params) => params["id"])
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为它:id是一个子路由参数。而且我无法使用,this.route.firstChild因为 firstChild 将是另一条路线,具体取决于是否显示InfoComponentDetailsComponent正在显示。

:idMasterComponent显示两个子组件之间发生变化时,我可以获得一个可观察的(of )吗?

这是我的路由模块:

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: "master",
                canActivate: [AuthenticationGuardService],
                children: [
                    {
                        path: "",
                        component: MasterComponent,
                        children: [
                            {
                                path: "",
                                component: InfoComponent
                            },
                            {
                                path: ":id",
                                component: DetailsComponent
                            }
                        ]
                    }
                ]
            }
        ])
    ],
    exports: [ …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

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

如何知道组件当前是否是路由器插座中加载的组件

我有一个带有延迟加载模块的应用程序。

在一个特定的延迟加载模块中,我有一个父组件,它具有router-outlet.

在该父组件中,我有一个按钮,它基本上使用共享服务(在延迟加载模块上提供)发出事件。

每个子组件(在 中加载router-outlet)都使用相同的共享服务订阅该事件!

我尝试使用英雄演示在以下plunkr 中重新创建我的用例。

打开开发工具以查看console消息。

  • 单击Heroes链接(默认情况下应选中)。
  • 单击列表中的英雄项目。
  • 单击Admin链接。
  • 单击Emit Test按钮(右上角导航)

您将在控制台中看到两个组件都收到了 Event,即使两个组件都不应该是“ Active ”!!!

1)这是设计使然吗?

2) 如何知道当前组件是否是活动组件?


总结/更新

根据我的理解,EventEmitter/Subject 可以被订阅多次,但是当取消订阅时你不能使用它/订阅它 AFAIK。

我的解决方案是:

1)在服务中,我结束了以下操作:

get(){
  if (!this.testEvent.closed) {
      this.testEvent.complete();
      this.testEvent.unsubscribe();
    }
    this.testEvent= new EventEmitter<void>();
    return this.testEvent;
  }
}
Run Code Online (Sandbox Code Playgroud)

2)在我订阅事件的每个组件中,我添加了 ngDestroy 并取消订阅事件:

  ngOnDestroy() {
    this.testEvent.unsubscribe();
  }
Run Code Online (Sandbox Code Playgroud)

eventemitter angular2-routing angular

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

heroku 中的 Angular2 路由

我在 heroku 中有一个 angular2 应用程序,但我遇到了路由器问题。在 localhost 中,一切都像魅力一样,但是当我部署到 heroku 并尝试通过任何不是索引的路由访问时,我收到 404 错误,如果我去索引,然后导航低谷页面路由正常发生,除非我重新加载页面,然后我得到另一个 404,这是 heroku 使用的 package.json 的一部分我 "heroku-prebuild": "npm install http-server -g", "heroku-postbuild": "ng build --target=production --environment=prod && rsync -a dist/* .", "start": "http-server dist/", 是否需要设置任何要在我的 Procfile 中使用的快速重写?

heroku node.js angular2-routing angular

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

Angular2在routerLink中设置当前路由的参数

虽然看起来微不足道,但我没有找到任何类似的案例这是我的路线:

{
    path: 'folder/:id',
    component: FolderComponent,
    children: [
        {
            path: 'edit/:form',
            component: 'EditorComponent'
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

当我在edit/form1页面上时,我找不到指定 routerLink 指令的方法,以便它只更改:form值,而不会丢失父:id值。现在,我需要这样做:

1 <a [routerLink]="['../../edit, 'form2']">Form2</a>升两级。这有效。但它不是那么优雅......

2我试过了['edit', 'form2'],它让我folder/:id/edit/form1/folder/form2

3如果我这样做['/edit', 'form2'],我会得到/folder/form2

4我什至尝试过['', 'form2'],我明白了/form2

编辑 :

5建议我尝试过['./edit', 'form2'],但它给了我folder/:id/edit/form1/edit/form2

准确地说,我的链接在里面editor-component.html,当前的网址是http://myapp.com/folder/:id/edit/form1

谢谢你的帮助

angular2-directives angular2-routing angular

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

Angular 2 - 内容未加载到路由器插座中

我对 Angular 2 还是很陌生,希望你们能帮助我。

我有一个相当简单的应用程序,有一个登录页面,成功登录后,用户被定向到一个带有侧菜单的页面。登录屏幕没有此侧边菜单。当用户注销时,他会再次被定向到登录页面。

问题是登录后侧菜单变得可见,但其他内容只有在刷新后才可见。注销也是一样,注销后页面是空白的,只有在刷新内容(登录页面)后才会显示。我可能做错了什么,但即使在查看其他问题后我也无法弄清楚。

这是我的路由:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { ProfileComponent } from './profile-component/profile-component.component'
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { LoginComponent } from './login/login.component';
import { LoggedInGuard } from './logged-in/logged-in.guard';

const appRoutes: Routes = [
  {path: 'profile', component: ProfileComponent, canActivate: [LoggedInGuard]},
  {path: 'login', component: LoginComponent},
  {path: '', component: LoginComponent},
  {path: '**', component: PageNotFoundComponent},
];

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

javascript angular2-routing angular

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

单击按钮时覆盖 routerLink

我有一个我正在制作的电子商务网站,我的目标是每个对象有多个输入点,例如,他们点击/点击卡片上除按钮之外的任何地方,它会在点击/点击的同时转到产品详细信息按钮将该项目添加到购物车。

目前,我可以将其转发到详细信息,即使点击按钮,它也会转发到详细信息。我想让按钮的动作覆盖路由器。

这是我目前的尝试:

<a *ngFor="let item of list"  >
<div class="col-lg-3 col-md-4 col-xs-6">
    <div class="card" [routerLink]="['/detail', item.item_number]">
    <img style ="width:170px;height:150px; display:block;"alt="170x150" src="{{item.item_image}}">

    <div class="card-block">
    <p class="card-text text-muted">{{item.item_name}}</p>
    <p class="card-text">Size:{{item.item_length}}</p>
    </div>

    <div class="container">        
    <button type="button" class="btn btn-primary btn-block" (click)="addItem()">Add<span [routerLink]="['/detail', item.item_number]" [class.disabled]="disabled ? true : null"></span></button>
    </div>
</div>
</div>
</a>
Run Code Online (Sandbox Code Playgroud)

我希望在按钮中嵌套禁用命令会起作用,但情况似乎并非如此。任何想法都会很棒!

angular2-template angular2-routing angular

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