0
我試圖在Aurelia CustomElement的CSS文件中使用AureliaUX變量。如果每個實例對於CSS中使用的變量具有相同的值,那麼效果很好,但對於每次使用自定義元素,我都無法使其具有不同的值。自定義元素的在自定義元素上爲每個實例使用不同值的AureliaUX
例子:
// View Model
// box.js
import {inject, bindable} from 'aurelia-framework';
import {AureliaUX} from 'aurelia-ux';
@inject(AureliaUX)
export class Box {
@bindable color = '#ff0000';
constructor(ux) {
this.ux = ux;
}
bind() {
this.setProperty();
}
colorChanged() {
this.setProperty();
}
setProperty() {
this.ux.design.boxColor = this.color;
}
}
<!-- View -->
<!-- box.html -->
<template styles.box>
<require from="./box.css#ux"></require>
<div>
<slot></slot>
</div>
</template>
/* CSS UX File */
/* box.css */
styles.box > div {
background-color: ${$design.boxColor};
}
在我的應用我然後使用自定義元素是這樣的:
<template>
<require from=".../box">
<box>This box should be red</box>
<box color="#00ff00">This box should be green</box>
<box color="#0000ff">This box should be blue</box>
</template>
釷預期的結果是每個盒子都有不同的顏色。當前的結果是每個框都獲得最後定義的顏色屬性的顏色,在這種情況下,每個框都是藍色的。
我該怎麼做才能讓顏色屬性是一個可綁定的屬性,並且針對每個Box實例?
需要注意的是,要使StyleEngine正常工作,必須從視圖中獲取關聯的JS文件,而不是CSS文件(例如,在您的示例中) – Ben