我想知道是否有一种在Angular2中注入接口的正确方法?(参见下文)
我认为这与接口上缺少的@Injectable()装饰器有关,但似乎不允许这样做.
问候.
当CoursesServiceInterface作为接口实现时,TypeScript编译器会抱怨"CoursesServiceInterface找不到名称":
import {CoursesServiceInterface} from './CoursesService.interface';
import {CoursesService} from './CoursesService.service';
import {CoursesServiceMock} from './CoursesServiceMock.service';
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
GlobalService,
provide(CoursesServiceInterface, { useClass: CoursesServiceMock })
]);
Run Code Online (Sandbox Code Playgroud)
但是使用CoursesServiceInterface作为接口:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
//@Injectable()
export interface CoursesServiceInterface {
getAllCourses(): Promise<Course[]>;//{ return null; };
getCourse(id: number): Promise<Course>;// { return null; };
remove(id: number): Promise<{}>;// { return null; };
}
Run Code Online (Sandbox Code Playgroud)
当service是一个类时,TypeScript编译器不再抱怨:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
@Injectable()
export class CoursesServiceInterface {
getAllCourses() : Promise<Course[]> { return null; …Run Code Online (Sandbox Code Playgroud) 我无法在 Ionic 5 中实现 canDeactivate 防护。以下是我的代码。
模型.ts
export interface isDeactivatable {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
Run Code Online (Sandbox Code Playgroud)
离开页面.guard.ts
export class LeavePageGuard implements CanDeactivate<isDeactivatable>{
canDeactivate(
component: isDeactivatable
): Observable<boolean> | Promise<boolean> | boolean {
console.log('LeavePageGuard');
return component.canDeactivate();
}
}
Run Code Online (Sandbox Code Playgroud)
测试页.ts
export class TestPage implements OnInit, isDeactivatable{
canDeactivate(): Observable<boolean> | Promise<boolean> | boolean {
console.log('canDeactivate in TestPage');
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
本地路由.Module.ts
const routes: Routes = [
{
path: '',
component: HomePage,
children:[
{
path: 'test',
loadChildren: () => …Run Code Online (Sandbox Code Playgroud) lazy-loading ionic-framework angular-route-guards candeactivate ionic5