在给定条件的情况下,我想异步地将一个组件附加到路由.
以下示例(可以是异步的)根据用户角色加载一个或另一个组件:
import { UserDashboardComponent } from './user-dashboard.component'
import { AdminDashboardComponent } from './admin-dashboard.component'
const role = 'admin' // Just for the example
const comp = role === 'admin' ? AdminDashboardComponent : UserDashboardComponent
const routes: Routes = [
{ path: '', component: comp },
]
Run Code Online (Sandbox Code Playgroud)
但是,假设我们想要从API检索角色,从而异步.有什么方法可以实现这一目标?
高阶组件(HOC)是React社区中常见的模式.如果您不知道什么是HOC,请参阅此文章
他们在Angular 2中有意义吗?如何制作HOC?任何例子?
我正在使用装饰器将变量添加到指令的隔离范围.但不知何故,它不起作用,我几乎尝试了一切.
精确指令是angular-ui之一:
.directive('accordionGroup', function() {
return {
require:'^accordion', // We need this directive to be inside an accordion
restrict:'EA',
transclude:true, // It transcludes the contents of the directive into the template
replace: true, // The element containing the directive will be replaced with the template
templateUrl:'template/accordion/accordion-group.html',
scope: {
heading: '@', // Interpolate the heading attribute onto this scope
isOpen: '=?',
isDisabled: '=?'
},
...
})
Run Code Online (Sandbox Code Playgroud)
$ decorator代码,我用它来更改模板并添加一个新变量:
mainModule.config(['$provide', function ($provide){
$provide.decorator('accordionGroupDirective', function($delegate) {
var directive = $delegate[0];
directive.templateUrl = "views/parts/accordion-unit.html"; …Run Code Online (Sandbox Code Playgroud)