Mocking out NavParams in Ionic3 gives back Propert

2019-07-22 15:53发布

问题:

export class NavParamsMock {

    static returnParam = null;
    public get(key): any {
        if (NavParamsMock.returnParam) {
            return NavParamsMock.returnParam
        }
        return 'default';
    }
    static setParams(value){
        NavParamsMock.returnParam = value;
    }
}

It's not working.

回答1:

As the error notes the data is missing from your class so it fails.

If you don't care and want to avoid these kind of errors then extend your class.

export class NavParamsMock extends NavParams

This will fill in the missing properties.

If you don't want to extend the full class you can just the data property as the error says

export class NavParamsMock {
    static returnParam = null;
    get data(){ return NavParamsMock.returnParam; }
    set data(params) { NavParamsMock.returnParam = params; } 
    public get(key): any {
        if (NavParamsMock.returnParam) {
            return NavParamsMock.returnParam
        }
        return 'default';
    }
    static setParams(value){
        NavParamsMock.returnParam = value;
    }
}