gpt4 book ai didi

aurelia - aurelia 中的自定义属性不起作用?

转载 作者:行者123 更新时间:2023-12-04 17:47:22 28 4
gpt4 key购买 nike

我正在学习 Aurelia 的工作原理,并且我正在尝试让一个简单的自定义属性工作。它所做的就是根据某些值的变化来改变 div 文本的颜色。

我有一个 div,它有:

    high.bind="changeColor"

在我的属性中,我有:
import {inject, customAttribute} from 'aurelia-framework';

@customAttribute('high')
@inject(Element)
export class High {
constructor(element) {
this.element = element;
}

valueChanged(newValue){
console.log(newValue);
if (newValue) {
this.element.classList.remove('highlight-yellow');
} else {
this.element.classList.add('highlight-blue');
}
}

在我的 View 模型中,我有:
import {high} from './highlightattribute'


export class Welcome{
heading = 'Welcome to the Aurelia Navigation App!';
firstName = 'John';
lastName = 'Doe';

get fullName(){
return `${this.firstName} ${this.lastName}`;
}

get changeColor(){
if (this.firstName == 'John'){
return false;
}
return true;
}
welcome(){
alert(`Welcome, ${this.fullName}!`);
}
}

当我更改名字时,我看不到在高自定义属性类中触发的 valueChanged 事件。

最佳答案

看起来您正在将高代码导入您的 View 模型而不是您的 View 。删除 ViewModel 中的这一行:

import {high} from './highlightattribute'

然后将此行添加到您的 View 中:
<require from="./highlightattribute"></require>

接下来,在 highlightattribute.js您要删除的文件 highlight-yellow并添加 highlight-blue ,因此您可能想要添加和删除相同的类。我也注意到您的 highlightattribute.js 中缺少括号。您发布的文件,但在复制代码时可能只是错过了。

让我知道这是否有助于解决问题。我已将包含您的代码的示例推送到此处: https://github.com/AshleyGrant/skeleton-navigation/tree/so-answer-20150416-01/src

关于aurelia - aurelia 中的自定义属性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29680191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com