为什么要给一个"摘要:真QUOT;说出一个网址? [英] Why give an "abstract: true" state a url?

查看:148
本文介绍了为什么要给一个"摘要:真QUOT;说出一个网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被周围的UI路由器摆弄今天在试图更好地理解离子脚手架一件事,我注意到的是,他们给标签URL的抽象状态。

I've be fiddling around with ui-router today in trying to better understand the scaffolding in Ionic and one thing that I noticed was that they give the abstracted state of "tabs" a url.

我用过抽象状态的唯一的时候,我用了一个空字符串作为URL,我注意到,如果我曾经无意中试图导航到一个抽象的状态(而不是孩子的状态)我得到的错误:

The only times I've ever used abstract states, I used an empty string as the url and I notice that if I've ever accidentally attempted to navigate to an abstracted state (as opposed to the child state) I get the error:

无法过渡到抽象状态[insertAbstractStateHere]

编辑:

而且,在实验中,当我尝试将URL指定给我的抽象状态(离子以外),并仍然呈现嵌套状态的看法,我得到一个大鹅蛋。什么都没有出现。

"Moreover, in experimenting, when I try to assign a url to my abstract state (outside of Ionic) and still render the nested state views, I get a big goose egg. Nothing shows up at all."

在上面引述的说法是假的!我在 Plunker 试图再次和嵌套状态并显示出来。

the above quoted statement is false! I tried it again in Plunker and the nested states did show up.

 angular.module('routingExperiments', ['ui.router'])
      .config(function($urlRouterProvider, $stateProvider) {

    $stateProvider

      .state('abstractExperiment', {
        abstract: true,
        url: '', //<--- seems as if any string can go here.
        templateUrl: 'abstractExperiment.html'
      })
      .state('abstractExperiment.test1', {
        url: '/test1',
        templateUrl: 'abstractTest1.html'
      });
  });

显然,我确实是做错了。所以我的新问题是:

Apparently I was indeed doing it wrong. So my new question is:

是否有任何理由,为什么人会使用一个名为状态,在使用抽象状态而不是一个空字符串,或者是它只是一种风格的选择吗?

Is there any reason why one would use a named state as opposed to an empty string in employing abstract states, or is it just a style choice?

推荐答案

您将使用抽象状态的原因是为了保持你定义的的,当你有你的网址不通航的一部分。例如,假设你有一个像下面这样的URL方案:

The reason you would use an abstract state is to keep your definition dry when you have a part of your url non-navigable. For example, say that you had a url scheme like the following:

/home/index
/home/contact

不过,对于您的设计无论是什么原因,该网址是无效的(即没有目的的页面):

However, for whatever reason in your design, this url was invalid (i.e. no purpose for a page):

/home

现在,你可以简单地创建两个国家针对这种情况,具有完整的URL,但此后你会写 /家庭/ 两次,和描述是有点多令人费解的。最好的办法,而不是是创建一个抽象家父母其中两个其他国家的孩子(对于UI路由器文档):

Now you could simply create two states for this situation, with the complete urls, however then you would be writing /home/ twice, and the description is a bit more convoluted. The best idea instead is to create a home abstract parent of which the two other states are children (for ui-router docs):

$stateProvider
    .state('parent', {url: '/home', abstract: true, template: '<ui-view/>'} )
    .state('parent.index', {url: '/index', templateUrl: 'index.html' })
    .state('parent.contact', {url: '/contact', templateUrl: 'contact.html' })

只需注意的父状态里面,我们分配一个模板,其唯一的孩子是一个用户界面视图。这确保了孩子们提供(可能是为什么你正在出现空白)。

Just notice that inside the parent state, we assign a template whose only child is a ui-view. This ensures that the children are rendered (and might be why yours is appearing blank).

有时你可能会注意到一个空白URL中使用抽象的状态。此设置的最佳用途是当你需要一个亲决心。例如,您可能需要为你的国家的一个子集,一些特定的服务器数据。因此,与其把同样的决心功能为您的每一个状态,你可以创建具有所需决心空白上级URL。如果你想分层控制器,其中母公司对视图没有用它也可能是有用的(不知道你为什么会想这一点,但它是合理的)。

Sometimes you might notice the use of an abstract state with a blank url. The best use of this setup is when you need a parental resolve. For example, you may require some particular server data for a subset of your states. So instead of putting the same resolve function into each of your states, you could create a blank url parent with the desired resolve. It could also be useful if you want hierarchical controllers, where the parent has no use for a view (not sure why you would want this, but it is plausible).

这篇关于为什么要给一个&QUOT;摘要:真QUOT;说出一个网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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