角度检查服务实例

时间:2019-03-04 13:26:23

标签: angular typescript angular-routing angular-components

我正在尝试实现canDeactivate防护。为此,我需要检查组件上服务实例的属性。但是,由于可能会使用不同的别名注入此服务,所以我尝试将所有属性检查为服务的实例。

canDeactivate(
    component: Component,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState ? : RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {

    let tabsService;

    const componentProps = Object.entries(component);

    console.log(componentProps);

    for (let i = 0; i < componentProps.length; i++) {
        if (componentProps[1] instanceof TabsService) {
            console.log(true);
            tabsService = componentProps[1];
            break;
        }

        console.log(componentProps[1] instanceof TabsService);
    }

    ...
}

但是,这将导致所有false个问题。enter image description here

我该怎么做?还是应该以其他方式获取实例?我知道用不同的别名来称呼一件事很不好,但这对于大量的DI和团队发展(IMHO)是不可避免的。

1 个答案:

答案 0 :(得分:3)

应该到处都是import argparse import os import sys import pandas as pd from sklearn.externals import joblib module_path = os.path.abspath('/opt/ml/code') if module_path not in sys.path: sys.path.append(module_path) from lib import training, prediction from data.transactions import raw if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--output-data-dir', type=str, default=os.environ['SM_OUTPUT_DATA_DIR']) parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR']) args = parser.parse_args() grid_models_dict =\ training.train_models_in_dict(raw_training_data=raw) joblib.dump(grid_models_dict, os.path.join(args.model_dir, "model")) def model_fn(model_dir): grid_models_dict = joblib.load(os.path.join(model_dir, "model")) return grid_models_dict def predict_fn(input_data, model): predicted = prediction.predict(input_data, model) return predicted ,而不是componentProps[i][1],不是吗?