角度路线在聊天应用中不起作用

时间:2018-03-07 15:03:28

标签: angular router

我在角度应用程序中遇到导航问题。当我登录时,聊天屏幕不会出现,但导航栏已成功登录。

这是route.ts

import ...

export const appRoutes: Routes = [
  { path: 'signup', component: SignupFormComponent},
  { path: 'login', component: LoginFormComponent},
  { path: 'chat', component: ChatroomComponent},
  { path: '', redirectTo: '/login', pathMatch: 'full'},
];

这是app.module.ts

imports: [
RouterModule.forRoot(appRoutes),
],

和login-form.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';

@Component({
 ...
})
export class LoginFormComponent{
 email: string;
 password: string;
 errorMsg: string;

constructor(private authService: AuthService, private router: Router) { }

login() {
 this.authService.login(this.email, this.password)
 .then(resolve => this.router.navigate(['chat']))
 .catch(error => this.errorMsg = error.message);
 }
}

当我登录应用程序时,不导航到/ chat。 Before After login

信息版

"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"angularfire2": "^5.0.0-rc.4",
"core-js": "^2.4.1",
"firebase": "^4.9.0",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"
 },
"devDependencies": {
"@angular/cli": "1.6.5",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
 }

1 个答案:

答案 0 :(得分:0)

如果您要导航到/chat,则必须将/传递给路由器:

.then(resolve => this.router.navigate(['/chat']))

如果您没有在/前置路由器将chat附加到当前路由。