【问题标题】:Aurelia Custom Elements Inside of Custom Elements & Sharing VariablesAurelia 自定义元素内部的自定义元素和共享变量
【发布时间】:2016-01-12 23:32:19
【问题描述】:

如何在自定义元素之间访问和共享变量?我有以下文件...

tip.html

<template>
    <div class="tip-container">
        <content select="tip-trigger"></content>
        <content select="tip-content"></content>
    </div>
</template>

tip.js

export class Tip {}

tip-trigger.html

<template>
    <span class="tip-trigger" click.trigger="showTip()">
        <content></content>
    </span>
</template>

tip-trigger.js

export class TipTrigger {
    showTip() {
        console.debug(this);
    }
}

提示内容.html

<template>
    <span class="tip">
        <content></content>
        <span class="tip__close  tip__close--default">×</span>
        <span class="tip__color"></span>
    </span>
</template>

提示内容.js

export class TipContent {}

在我的Tip 类中,我想要一个变量名visible。当showTip 被触发时,visible 将设置为 true,然后我将使用它在tip-content.html 中添加一个类。我如何在这些自定义元素之间共享变量来做到这一点?

这个想法是创建一个元素来显示提示弹出窗口,其中任何类型的内容都可以作为触发器,并且在触发时可以显示任何类型的内容。基本示例:

<tip>
  <tip-trigger><button>?</button></tip-trigger>
  <tip-content><div>Here is some helpful info...</div></tip-content>
</tip>

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    Here 是您在 Plunker 中遇到的问题的解决方案。

    请注意,tip-triggertip-content 元素只是模板的可替换部分。它们本身不需要成为组件(这让我在"original" custom elements article 中很困惑)。

    app.html:

    <template>
      <require from="tip"></require>
      <tip>
          <tip-trigger><button>?</button></tip-trigger>
          <tip-content><div>Here is some helpful info...</div></tip-content>
      </tip>
    </template>
    

    tip.html:

    <template>
        <div class="tip-container">
          <div>
             <div click.trigger="showContent()">
               <content select="tip-trigger"></content>
             </div>
          </div>
          <div show.bind="contentVisible">
            tip content:
            <content select="tip-content"></content>
          </div>
        </div> 
    </template>
    

    tip.js:

    export class Tip {
      showContent(){
        this.contentVisible = !this.contentVisible;
      }
    }
    

    【讨论】:

    • 这要简单得多。谢谢!
    • 假设我想为&lt;tip-trigger&gt;&lt;tip-content&gt; 添加属性。那可能吗?如果是这样,我将如何访问它们?这是您需要让它们本身成为组件的地方吗?如果是这样,那让我回到我最初的问题,即如何访问组件之间的变量。
    • 是的,在这种情况下,您将需要 tip-triggertip-content 作为组件。请参阅 cmets 到 @David M. Brown 对组件之间通信的回答。
    【解决方案2】:

    你只需要把Tip变成一个类似服务的类并导入吗?

    export class Tip {
        constructor() {
            this.visible = false;
        }
        show() {
            this.visible = true;  // Or whatever to show the content
        }
        hide() {
            this.visible = false;
        }
    }
    

    然后:

    import {inject} from 'aurelia-framework';  
    import {Tip} from './tip';
    
    @inject(Tip)
    export class TipTrigger {
        constructor(tip) {
            this.tip = tip;
        }
        showTip() {
            this.tip.show();
            // Or I suppose you could access 'visible' directly
            // but I like the implementation details a method call offers.
        }
    }
    

    *免责声明:这是未经测试的。

    【讨论】:

    • 那是缺失的部分。谢谢!
    • @David M. Brown 我认为如果您在视图中添加 2 个提示,它不会起作用,因为注入应该创建单例(除非您在提示中使用 transient 装饰器)。但事实是it woks。如果您使用不同的类来控制提示视图it really behaves as a singleton。好奇...
    • 我也有同样的担忧。我仍然非常了解 Aurelia 和相关的东西是如何工作的。
    • @DaniCE +1 为 plnkr
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多