【发布时间】:2018-07-09 10:38:38
【问题描述】:
我有一个名为 loading-bar 的自定义元素,它在我的应用程序的许多页面中使用。其目的是在加载、下载内容时显示状态消息,并为后端操作提供响应消息。
加载-bar.html:
<template show.bind="visible">
<i class="fa fa-circle-o-notch fa-spin fa-fw" if.bind="type==1"></i>
<i class="fa fa-check fa-fw" if.bind="type==2"></i>
<span id="loading-text">${message}</span>
</template>
loading-bar.ts:
import { customElement, bindable, bindingMode, inject } from 'aurelia-framework';
@customElement('loading-bar')
export class LoadingBarCustomElement {
private visible = false;
@bindable({ defaultBindingMode: bindingMode.twoWay }) message;
@bindable({ defaultBindingMode: bindingMode.twoWay }) type = 1;
constructor() {
this.visible = false;
}
messageChanged(newValue, oldValue) {
if (newValue && newValue !== '')
this.visible = true;
else
this.visible = false;
}
}
目前所有使用加载栏的页面都在其 html 中声明了此元素:
<loading-bar message.bind="loadMsg" type.bind="loadType"></loading-bar>
loading-Bar 是通过改变每个页面中的 loadMsg 和 loadType 局部变量来控制的。
我想做的是在适当的位置声明加载栏 html,并能够(从任何页面)调用诸如“showBar(msg,type)”之类的方法,该方法将影响全局声明的加载-酒吧。
我的第一个方法是在 app.html 中声明加载栏(就像在此处声明我的导航栏一样)并注入一个类(在所有页面的 ViewModel 中),其中包含 showBar(msg, type ) 方法来控制加载栏。
我不确定这是否是正确的前进方式,或者它的最佳实施方式,希望能得到一些帮助。
【问题讨论】: