标签: angular-routing

如何使用带有路由器插座的 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 中 onSameUrlNavigation 属性的用途是什么(版本大于 5)

最近我听说了一个名为onSameUrlNavigation的属性,您可以在其中将其设置为“重新加载”。我在谷歌上搜索过它,但没有多少文章展示了该属性的使用。有人可以帮助我提供一个实时示例,说明我们究竟需要在哪里使用该属性。

@ngModule({
 imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: ‘reload’})],
 exports: [RouterModule],
 })
Run Code Online (Sandbox Code Playgroud)

angular-routing angular

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

使用赛普拉斯测试 Angular 时如何防止整页重新加载?

当我为 Angular 应用程序编写 Cypress e2e 测试时,我经常使用这样的visit()命令:

.visit('/foo/bar')

这样就完成了工作,即 Cypress 导航到/foo/bar,但整个应用程序会重新加载。这非常慢,并且不会模仿实际的用户行为。

是否可以在不重新加载整页的情况下导航/访问 Angular 应用程序?

我确实尝试过:

cy.window().then((win) => {
   win.history.pushState({}, '', '/foo/bar')
})
Run Code Online (Sandbox Code Playgroud)

但是 angular 对此没有反应。

angular-routing angular cypress

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

ngmodule 不是子类型 ngmoduletype

我使用 angular 9 并且我想做延迟加载 app-routing

{
    path: '', loadChildren: () => import("./components/login/login.module")//.then(m =>
     // m.LoginModule)
  }
Run Code Online (Sandbox Code Playgroud)

在我创建登录模块之后:

@NgModule({
    declarations: [LoginComponent],
    imports: [
        CommonModule,
        FormsModule,
        LoginModuleRouting

    ],
    providers:[]
})
export class LoginModule { }
Run Code Online (Sandbox Code Playgroud)

和路由:

const routes: Routes = [
  {
    path: '', component: LoginComponent,

  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})


export class LoginModuleRouting { }
Run Code Online (Sandbox Code Playgroud)

问题是,当我打电话ng serve并继续访问 ` http://localhost:4200/ 时,我得到了这个异常:

core.js:6237

 ERROR Error: Uncaught (in promise): Error: ASSERTION ERROR: NgModule '[object Module]' is not a …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular-module angular angular9

9
推荐指数
3
解决办法
7071
查看次数

Angular 2:无法解析Router的所有参数

目标

在不失去理智的情况下使路由工作.

错误

Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?)
Run Code Online (Sandbox Code Playgroud)

app.routing.ts

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

import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

export const appRoutes: Routes = [
    { path: '', redirectTo: 'customers', pathMatch: 'full' },
    { path: 'customers', component: CustomerComponent },
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { NgModule } from '@angular/core';
import { …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular

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

Resolve Guard:如果没有找到数据,则从路由中解析observable的正确方法

如果没有找到数据,angular.io上的示例中的resolve方法返回一个promise或将应用程序导航到特定路由.

resolve(route: ActivatedRouteSnapshot): Promise<any> {
let id = +route.params['id'];
return this.cs.getCrisis(id).then(crisis => {
  if (crisis) {
    return crisis;
  } else { // id not found
    this.router.navigate(['/crisis-center']);
    return false;
  }
});
}
Run Code Online (Sandbox Code Playgroud)

假设,getCrisis函数返回一个observable:

resolve(route: ActivatedRouteSnapshot): Observable<any> {
  let id = +route.params['id'];
  return this.cs.getCrisis(id).take(1)
}
Run Code Online (Sandbox Code Playgroud)

在可观察的情况下.我怎么知道,没有任何回报,因为我正在处理流?在resolve函数中处理这种情况的最佳模式是什么?

我知道,我可以从组件中使用router.navigate方法,但是想要正确使用路由器解析防护.

angular-routing angular

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

在angular4中重新加载具有不同路径参数的组件

我正在使用angular2路由.当用户在搜索字段中重新输入值并单击搜索时,需要重新加载相同的组件,但具有不同的路由参数.

<button (click)="onReload()">Search</button>

onReload() {
this.router.navigate(['results',this.location]);
}
Run Code Online (Sandbox Code Playgroud)

这是我的ResultsComponent的路径路径

{ path:'results/:location', component:ResultsComponent}
Run Code Online (Sandbox Code Playgroud)

此函数更改URL,但不会重新初始化组件.

在angularjs中,ui-router我可以像这样实现它.

$state.go($state.current, location, {reload: true});
Run Code Online (Sandbox Code Playgroud)

我如何在angular4中执行此操作?

angular-routing angular angular-router

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

相对路径不起作用

当我在` http:// localhost/overview '时,模块沿着这条路径加载:

http://localhost/app/modules/overview/overview.module.js

但是当我转向页面http://localhost/overview/users然后按F5(刷新页面)时,我收到错误:

Error: Unexpected token <
  Evaluating http://localhost/overview/app/modules/overview/overview.module
Run Code Online (Sandbox Code Playgroud)

发生错误是因为URL不正确,他应该是这样http://localhost/app/modules/overview/overview.module.

如何让它正常工作?

这是项目结构:

在此输入图像描述

这是systemjs tsconfig:

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true
  }
}
Run Code Online (Sandbox Code Playgroud)

这是systemjs配置:

(function (global) {
    System.config({
        baseURL: "/",
        paths: {
            'npm:': '/node_modules/'
        },
        map: {
            app: 'app',

            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': …
Run Code Online (Sandbox Code Playgroud)

typescript angular-routing systemjs angular angular-router

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

获取组件中的路由参数

我正在研究 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 中的路径?

如何将之前的路线作为 Angular 中的路径?

我尝试通过此链接使用解决方案,但它不会在首页加载时执行。

constructor(router: Router) {
  router.events
  .pipe(filter(event => event instanceof NavigationEnd))
  .subscribe((event: NavigationEnd) => {
    console.log('prev:', event.previousUrl);
    this.previousUrl = event.url;
  });
}
Run Code Online (Sandbox Code Playgroud)

我使用了RoutesRecognizedNavigationEnd,但是它们不起作用。

我在 constructor 和中调用了这个方法ngOnInit,但是它们都没有在首页加载时执行。

有没有办法获取之前的URL?

谢谢。

javascript routes typescript angular-routing angular

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