Aurelia中的可选视口

4

我正在设置一个具有多个视口的应用程序。然而,我希望其中一个视口是可选的,即如果在某个路由中没有定义视口,则不应该渲染它。但是,如果我尝试这样做,Aurelia会抛出错误。是否有另一种方法使视口可选?

2个回答

2
如果Aurelia没有内置支持,您可以将不需要的视口点配置为具有以下逻辑的模块:
import {noView, inject} from 'aurelia-framework';

@noView
@inject(Element)
export class HiddenViewPort {
  constructor(element) {
    this.element = element;
  }

  activate() {
    // hide the router-view element
    this.element.parentNode.classList.add('aurelia-hide');
  }

  deactivate() {
    // show the router-view element
    this.element.parentNode.classList.remove('aurelia-hide');
  }
}

2
这还有效吗?首先,@noView装饰器会导致错误,但我可以使用一个空的(inlineView)模板来解决它。我的问题是:注入的元素不是<router-view>元素的父元素(正如我所期望的那样),而是具有设置为aurelia-app的元素。因此,在我的情况下,aurelia-hide被分配给其父级,即<body>。我在gist.run中重现了这个问题:https://gist.run/?id=54748b3d8043726168a7b76d99a5865e 如果您查看它,aurelia-hide类被设置在<html>元素上。 - Daniel
我遇到了同样的问题。有人知道解决方法吗? - JoshKraker

0
据我所知,不支持可选视口。我们的解决方法是使用一个没有任何内容的模块:
import {inlineView} from 'aurelia-templating';

@inlineView('<template></template>')
export class EmptyViewModel {
}

使用@noView将具有相同的效果。 - balazska

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接