Angular 1.5-UI-Router,解析对象返回未定义的值 [英] Angular 1.5 - UI-Router, Resolve Object returning undefined values

查看:260
本文介绍了Angular 1.5-UI-Router,解析对象返回未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从预装到组件控制器中的ui路由器获取解析数据(通过解析对象).属性显示在组件的 this 对象中,但所有属性均未定义.为什么会这样呢?我已经被卡住了一段时间了...

I am unable to get resolved data (via resolve object) from ui router pre-loaded into a component controller. the properties are showing up in the component's this object, but all properties are undefined. Why is this happening? I have been stuck on this for a while now...

这是组件:

  .component('wLoans', {
    template: require('./loans.html'),
    controller: LoansController,
    bindings: {
      settings: '<',
      labels: '<'
    }
  });

this.settings和this.labels出现,但是它们是未定义的.当我在下面的resolve.settings内进行console.log时,我可以看到promise本身.在下面的承诺中, value 包含我想要的返回数据:

this.settings and this.labels appear, but they are undefined. when I console.log inside resolve.settings below, I can see the promise itself. in the promise below, value contains the return data I want:

以下是我正在使用UI路由器的状态:

below is the state I am working on with UI router:

{
      name: 'loans',
      parent: 'app',
      url: '/loans',
      data: {
        authRequired: true
      },
      views: {
        content: {
          template: '<w-loans></w-loans>'
        }
      },
      resolve: {
        labels(LabelService) {
          'ngInject';
          return LabelService.fetch();
        },
        settings(SettingsService) {
          'ngInject';
          console.log(SettingsService.fetch());
          return SettingsService.fetch()
        },
        module($q, $ocLazyLoad, LabelService) {
          'ngInject';
          return $q((resolve) => {
            require.ensure([], (require) => {
              let mod = require('pages/loans');
              $ocLazyLoad.load({ name: mod.name });
              resolve(mod.name, LabelService.fetch());
            }, 'loans');
          });
        }
      }
    }

这是从路由中调用的服务中获取的功能:

here is the fetch function from the service that's being called in the route:

function fetch() {
        // return $http.get(require('!!file!mocks/settings.json'), {
    return $http.get(`${DEV_API_URL}/settings`, {
      cache: settingsCache,
      transformResponse: [
        ...$http.defaults.transformResponse,
        transformResponse
      ]
    })
      .then(({ data }) => {
        angular.extend(model, data);
        settingsCache.put('store', model);
        return model;
      });
  }

感谢您的帮助!

更新:使用ui路由器1.0.0-beta.1

UPDATE: using ui router 1.0.0-beta.1

推荐答案

只要您没有使用ui-router的1.0.0(当前为beta)版本,就无法直接使用这些组件.对于beta版本,请参阅以下讨论: https://github.com/angular-ui/ui-router/issues/2627 .还有一个关于如何使用带有解析的组件的路由的教程: https://ui-router.github.io/tutorial/ng1/hellogalaxy

As long as you are not using the 1.0.0 (currently beta) version of the ui-router there is no way to directly use the components. For the beta version, please see this discussion: https://github.com/angular-ui/ui-router/issues/2627. There is also a tutorial on how to use routing to components with resolves: https://ui-router.github.io/tutorial/ng1/hellogalaxy

您在这里正在创建一个名为 loans 的状态,该状态具有隐式的控制器和范围,将分配已解析的数据.然后,您可以在模板中实例化组件,而无需传递任何必需的属性.

What you are doing here is creating a state named loans with an implicit controller and scope which will have the resolved data assigned. In the template you then instantiate your component without passing any of the attributes required.

因此,如果您想将组件与0.2.x ui路由器一起使用,则需要添加一些虚拟控制器,该虚拟控制器获取注入的已解析属性,然后通过模板将虚拟控制器的值传递给组件,例如

So if you want to use your component with the 0.2.x ui-router, you need to add some dummy controller which gets the resolved properties injected and then pass the values of the dummy controller into the component via the template like

<w-loans settings="settings" .../>

取决于您的ui路由器版本(0.3.x?)有多新,您可以使用新引入的 $ resolve 属性来避免创建这样的虚拟控制器:

Depending on how new your version of the ui-router is (0.3.x?), you can probably use the newly introduced $resolve property to avoid creating a dummy controller like this:

<w-loans settings="$resolve.settings" ...></w-loans>

此处的详细信息: https://github.com/angular-ui/ui-router/issues/2793#issuecomment-223764105

这篇关于Angular 1.5-UI-Router,解析对象返回未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆