Angular ui路由器所有状态的多个命名视图

Angular ui router multiple named views for all states(Angular ui路由器所有状态的多个命名视图)
本文介绍了Angular ui路由器所有状态的多个命名视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以为所有状态编写多个命名视图,最好的例子是当我希望导航栏和页脚出现在所有路线中时.

I want to know if there is any way to write multiple named view for all states, the best example is when i want the nav bar and footer to appear in all routes.

$stateProvider
  .state('home',{
    views: {
      'home': {
        templateUrl: 'home.html',
        controller: controller
      },
      'nav': {
        templateUrl: 'nav.html',
        controller:controller
      },
      'footer': {
        templateUrl: 'footer.html',
        controller: controller
      },
    }
  })

我不想使用 ng-include,因为在这种情况下,导航和页脚在主状态解决之前显示.

I dont want to use ng-include, because the nav and the footer is showing before the home state is resolved in this case.

推荐答案

可以,其实写在ui-router的管理指南多个命名视图.

Yes you can, its actually written in the ui-router's guide on how to manage Multiple Named Views.

首先,您需要在抽象状态下定义一组特定的命名视图,包括您将放置所有内容视图(例如 home.html)的视图,并将其放入无名视图中查看(空字符串).

First, you need to define a specific set of named views in an abstract state, including the view where you would put all your content views such as your home.html and put it in a nameless view (empty string).

您可能已经注意到,下面的演示显示了一个名为 app 的根状态,它也是一个抽象状态(这意味着您无法在此状态下导航).它有三个视图,每个视图代表一个对应于 index.html 中定义的 ui-view 的名称.

As you may have noticed, the demo below shows a root state named app, which is also an abstract state (this means you can't navigate in this state). It has three views, each represents a name that corresponds to the ui-views defined in the index.html.

在无名视图中,包含 content.html 有一个无名 ui-view 将代表 app 状态.通过这样做,如果您在 app 状态下添加这些状态,您可以将 nav.htmlfooter.html 共享到所有状态.app.homeapp.items 状态就是一个例子.要了解更多信息,请阅读我在上面添加的链接.

Within the nameless view, contains the content.html that has a nameless ui-view that will represent all the child states of the app state. By doing this, you can share the nav.html and footer.html to all your states if you add these states under the app state. An example to this would be the app.home and app.items state. To learn more about this, read the link I've added above.

演示

Javascript

$urlRouterProvider.otherwise('/home');

$stateProvider.state('app', {
  abstract: true,
  views: {
   nav: {
     templateUrl: 'nav.html',
     controller: 'NavController as Nav'
   },

   '': {
     templateUrl: 'content.html',
     controller: 'ContentController as Content'
   },

   footer: {
     templateUrl: 'footer.html',
     controller: 'FooterController as Footer'
   }

  }
})

.state('app.items', {
  url: '/items',
  templateUrl: 'items.html',
  controller: 'ItemsController as Items'
})

.state('app.home', {
  url: '/home',
  templateUrl: 'home.html',
  controller: 'HomeController as Home'
});

HTML

index.html

<ui-view name="nav"></ui-view>
<ui-view></ui-view>
<ui-view name="footer"></ui-view>

content.html

<hr>
<ui-view></ui-view>
<hr>

这篇关于Angular ui路由器所有状态的多个命名视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Swipable Vuetify Tabs with router(带路由器的可滑动Vutify标签)
Vue router - how to have multiple components loaded on the same route path based on user role?(VUE路由器-如何根据用户角色将多个组件加载到同一路由路径上?)
Router Link in VUE JS is not showing active(VUE JS中的路由器链路未显示为活动状态)
Can i import single file component using Vue and Vue Router CDN?(我可以使用VUE和VUE路由器CDN导入单个文件组件吗?)
Vue 3 - Get access to router-view instance in order to call child methods(VUE 3-访问路由器视图实例以调用子方法)
How to replace one parameter in Vuejs router(如何替换Vuejs路由器中的一个参数)