aurelia中的自定义属性无效?

JD.*_*JD. 5 aurelia

我正在学习Aurelia如何工作,我正在尝试使用简单的自定义属性.它所要做的就是根据某些值的变化来改变div文本的颜色.

我有一个div:

    high.bind="changeColor"
Run Code Online (Sandbox Code Playgroud)

在我的属性中我有:

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');
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的视图模型中,我有:

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}!`);
 }
}
Run Code Online (Sandbox Code Playgroud)

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

Ash*_*ant 8

您似乎将高代码导入到viewmodel而不是视图中.在ViewModel中删除此行:

import {high} from './highlightattribute'
Run Code Online (Sandbox Code Playgroud)

然后将此行添加到您的视图中:

<require from="./highlightattribute"></require>
Run Code Online (Sandbox Code Playgroud)

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

如果这有助于解决问题,请告诉我.我已将您的代码示例推送到此处:https://github.com/AshleyGrant/skeleton-navigation/tree/so-answer-20150416-01/src

  • 非常感谢阿什利.我第一次看Aurelia所以我非常感谢你的帮助.对我来说没有更多的角度:). (2认同)