anguar2 nativescript错误“TypeError:undefined不是对象(评估'this.router')”

时间:2017-09-06 06:25:26

标签: angular2-routing nativescript

我是Angular2 Native脚本编程的新手......我需要将一个页面导航到另一个页面。我坚持使用typeError:“undefined不是一个对象(评估'this.router')”plz帮我解决.. 我的代码是..

// page1.ts

public constructor(private router: Router) { }
getMyDrawing(args) {
    let pad = this.DrawingPad.nativeElement;
    let img: Image = this.signImage.nativeElement;
    let drawingImage;
    pad.getDrawing().then
    (
        (data) => {
            console.log(data);
            drawingImage = data;
            img.src=data;
            this.router.navigate(['page2']);
        }

    );
}

// routing.ts

import { DrawingPadExample } from "./app.component";
import { Page2Component } from "./app.page2";

export const routes = [
  {   path: "drawing-pad-example", component: DrawingPadExample},
   {   path: "page2", component: Page2Component }
];

 export const navigatableComponents = [
 DrawingPadExample,
 Page2Component
 ];

// module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-
angular/nativescript.module";
import { DrawingPadExample } from "./app.component";
import { routes, navigatableComponents } from "./app.routing";
import { NativeScriptRouterModule } from "nativescript-
angular/router";
@NgModule({
    bootstrap: [
        DrawingPadExample
    ],
    imports: [
        NativeScriptModule,
        NativeScriptRouterModule,
        NativeScriptRouterModule.forRoot(routes)
    ],
    declarations: [
        DrawingPadExample,
        ...navigatableComponents
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AppModule { }

3 个答案:

答案 0 :(得分:1)

当你说导航无法正常工作时,你什么都没发生并且没有产生错误?

我建议你尝试将回调放在zoneCallback中。

public constructor(private ngZone: NgZone, private router: Router){
}

public getMyDrawing(args) {
    let pad = this.DrawingPad.nativeElement;
    let img: Image = this.signImage.nativeElement;
    let drawingImage;

    pad.getDrawing().then((data) => {
        this.ngZone.run(() => { 
            console.log(data);
            drawingImage = data;
            img.src=data;
            this.router.navigate(['page2']);
        });
    });
}

答案 1 :(得分:0)

@JBR,不,我指的是使用构造函数的依赖注入,例如,

constructor(private router: Router) {}

将该行添加到page1组件。

<强> 更新

替换此

function(data) {
        console.log(data);
        drawingImage = data;
        img.src=data;
        this.router.navigate(['page2']);
    }

带箭头功能

(data) => {
        console.log(data);
        drawingImage = data;
        img.src=data;
        this.router.navigate(['page2']);
    }

答案 2 :(得分:0)

在路由器的构造函数中添加实例。

constructor(private router : Router){}

因为您正在尝试访问错误typeError:"undefined is not an object (evaluating 'this.router')" this.router尚未启动