Angular2 - 没有 LoggedInGuard 的提供者
Angular2 - No provider for LoggedInGuard
我正在制作 Anuglar2 rc-4 网络应用程序。我正在尝试限制路由,但是当我添加我的登录守卫时,它告诉我没有提供程序。我的正常登录服务代码工作正常,就在我尝试添加登录守卫和 canActive 路由时。关于如何解决此错误的任何想法?
routes.ts
import { RouterConfig } from '@angular/router';
import { Home } from './components/home/home';
import { Login } from './components/login/login';
import { DashBoard } from './components/dashboard/dashboard';
import { User } from './components/user/user';
import { Store } from './components/store/store';
import { LoggedInGuard } from './models/logged-in.guard.ts';
export const routes: RouterConfig = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: Home, canActivate: [LoggedInGuard] },
{ path: 'login', component: Login },
{ path: 'dashboard', component: DashBoard },
{ path: 'user', component: User },
{ path: 'store', component: Store },
{ path: '**', redirectTo: 'home' }
];
boot.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { FormBuilder } from '@angular/common';
import { provideRouter } from '@angular/router';
import { HTTP_PROVIDERS, JSONP_PROVIDERS } from '@angular/http';
import { App } from './components/app/app';
import { routes } from './routes';
bootstrap(App, [
HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes)
]);
记录-in.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LoginService } from '../services/login.service';
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private ls: LoginService) {}
canActivate() {
return this.ls.isLoggedIn();
}
}
您必须将 LoggedInGuard
服务注入您的应用程序 bootstrap 函数
bootstrap(App, [
HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes),
LoggedInGuard // injected LoggedInGuard service provider here
]);
Note: This answer would not be the same for the current angular 2 updated version(rc.6), When you upgrade you need to create a
NgModule
where you are going to wrap up all the dependencies in
single place, that time LoggedInGuard
will go inside providers
option
of `NgModule
`
我正在制作 Anuglar2 rc-4 网络应用程序。我正在尝试限制路由,但是当我添加我的登录守卫时,它告诉我没有提供程序。我的正常登录服务代码工作正常,就在我尝试添加登录守卫和 canActive 路由时。关于如何解决此错误的任何想法?
routes.ts
import { RouterConfig } from '@angular/router';
import { Home } from './components/home/home';
import { Login } from './components/login/login';
import { DashBoard } from './components/dashboard/dashboard';
import { User } from './components/user/user';
import { Store } from './components/store/store';
import { LoggedInGuard } from './models/logged-in.guard.ts';
export const routes: RouterConfig = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: Home, canActivate: [LoggedInGuard] },
{ path: 'login', component: Login },
{ path: 'dashboard', component: DashBoard },
{ path: 'user', component: User },
{ path: 'store', component: Store },
{ path: '**', redirectTo: 'home' }
];
boot.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { FormBuilder } from '@angular/common';
import { provideRouter } from '@angular/router';
import { HTTP_PROVIDERS, JSONP_PROVIDERS } from '@angular/http';
import { App } from './components/app/app';
import { routes } from './routes';
bootstrap(App, [
HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes)
]);
记录-in.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LoginService } from '../services/login.service';
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private ls: LoginService) {}
canActivate() {
return this.ls.isLoggedIn();
}
}
您必须将 LoggedInGuard
服务注入您的应用程序 bootstrap 函数
bootstrap(App, [
HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes),
LoggedInGuard // injected LoggedInGuard service provider here
]);
Note: This answer would not be the same for the current angular 2 updated version(rc.6), When you upgrade you need to create a
NgModule
where you are going to wrap up all the dependencies in single place, that timeLoggedInGuard
will go insideproviders
option of `NgModule
`