10

我只是在使用 Aurelia 中的自定义元素功能,并尝试创建一个“进度条”元素。

进度条.js

import {customElement, bindable} from 'aurelia-framework';
@customElement('progress-bar')
export class ProgressBar
{
//do stuff//
}

进度条.html

<template>
  <link rel="stylesheet" type="text/css" href="src/components/progress-bar.css">
  <div class='progress-bar' tabindex=0 ref="bar">bloo</div>
</template>

test.html(相关部分)

  <require from="./components/progress-bar"></require>
  <progress-bar ref="pb">a</progress-bar>

所有这些都显示得很好。但是我正在努力让主页可以调用某些函数或更改元素上的某些属性,然后应该在进度条本身上做一些事情。

我试图在progress-bar.js 中创建一个函数'doSomething',但我无法在test.js 中访问它。

添加到progress-bar.js

doSomething(percent, value) {
  $(this.bar).animate('width: ${percent}%', speed);
}

在 test.js 里面

clicked() {
   console.log(this.pb); // this returns the progress bar element fine
   console.log(this.pb.doSomething); //this returns as undefined
   this.pb.doSomething(percent, value); // this fails outright with error: typeError - doSomething is not a function
}

接下来,我尝试在进度条元素中设置自定义属性,并可能使用 valueChange 来更改 div。

在progress-bar.js里面

@bindable percent=null;
@bindable speed=null;

测试.js

clicked() {
this.pb.percent=50; //this updated fine
this.pb.speed=1500; //this updated fine

}

那里没问题,几乎就在那里。但是如何设置一个处理程序以在属性更改时调用?

我找到了一个具有以下语法的教程:

@bindable({
  name:'myProperty', //name of the property on the class
  attribute:'my-property', //name of the attribute in HTML
  changeHandler:'myPropertyChanged', //name of the method to invoke on property changes
  defaultBindingMode: ONE_WAY, //default binding mode used with the .bind command
  defaultValue: undefined //default value of the property, if not bound or set in HTML
})

但我似乎无法在我的progress-bar.js 中使用该代码。添加后页面无法正确呈现。Gulp 似乎没有返回任何错误消息,但浏览器控制台返回此错误:

ERROR [app-router] Router navigation failed, and no previous location could be restored.

当我的页面某处出现语法错误时,我通常会收到以下消息。

这里有很多我不确定的事情:

这是自定义元素的正确用途吗?我们可以创建包含函数的自定义元素吗?我们可以创建具有自定义属性的自定义元素,然后在其值更改时触发事件吗?

很抱歉没有发布整个代码,因为我在尝试不同的东西时有很多变体。

4

3 回答 3

17

使用 Aurelia,您可以在组件中使用此约定: yourpropertynameChanged()

import {customElement, bindable, inject} from 'aurelia-framework';
import $ from 'jquery';

@customElement('progress-bar')
@inject(Element) 
export class ProgressBar
{
    @bindable percent;
    @bindable speed;

    constructor(element){
        this.element = element;
    }

    percentChanged(){
        doSomething(this.percent, this.speed);
    }

    speedChanged(){
        doSomething(this.percent, this.speed);
    }

    doSomething(percent, value) {
        $(this.element).animate('width: ${percent}%', value);
    }
}

您不需要doSomething()从外部访问。
但您只需要更改属性:

<progress-bar percent="${pb.percent}" speed="${pb.speed}">a</progress-bar>
于 2015-07-06T09:43:03.540 回答
4

我采用了一种更简单的惰性方法,并且正在使用nprogress,您可以使用nprogress.set(0.4),但我使用的是默认的“正在发生的事情”行为。

import nprogress from 'nprogress';
import {bindable, noView} from 'aurelia-framework';

@noView
export class LoadingIndicator {
  @bindable loading = false;

  loadingChanged(newValue){
    newValue ?
      nprogress.start() :
      nprogress.done();
  }
}
于 2015-07-08T10:09:59.950 回答
1

首先将一些对象绑定到您的自定义元素,例如“config”:

<custom-element config.bind="elementConfig"></custom-element>

然后在您的页面js文件中:

someFunction(){ this.elementConfig.elementFunction(); }

并在您的自定义元素中添加如下函数:

@bindable({ name: 'config', attribute: 'config', defaultBindingMode: bindingMode.twoWay });
export class JbGrid {
    attached() {
        this.config.elementFunction = e=> this.calculateSizes();
    }
    calculateSizes() {
    //your function you want to call
    }
}

现在您可以访问函数中的自定义元素“this”。

于 2017-04-15T13:14:14.750 回答