官术网_书友最值得收藏!

CSS class functions

The first error we encounter when viewing the application in the browser is:

Property or method "balanceClass" is not defined

The first error is with regards to both the balanceClass and activeClass functions we use. Both of these functions add CSS classes based on the data of the person, which does not change once the component has been rendered.

Because of this, we are able to use the caching found in Vue. Move the methods across to the component but put them in a new computed object, instead of the methods one.

With components, a new instance is created every time it is called, so we can rely on the person object we passed in via a prop and no longer need to pass the person into the function. Remove the parameter from the function and the view—also update any reference to person inside the function to this.person to reference the object stored on the component:

      computed: {
/**
* CSS Classes
*/
activeClass() {
return this.person.isActive ? 'active' :
'inactive';
},

balanceClass() {
let balanceLevel = 'success';

if(this.person.balance < 2000) {
balanceLevel = 'error';
} else if (this.person.balance < 3000) {
balanceLevel = 'warning';
}

let increasing = false,
balance = this.person.balance / 1000;

if(Math.round(balance) == Math.ceil(balance)) {
increasing = 'increasing';
}

return [balanceLevel, increasing];
}
},

The part of our component template that utilizes this function should now look like:

      <td v-bind:class="balanceClass">
{{ format(person, 'balance') }}
</td>
主站蜘蛛池模板: 政和县| 武穴市| 霞浦县| 商城县| 安宁市| 诸城市| 四川省| 五河县| 枣强县| 阳新县| 嘉禾县| 衡南县| 利辛县| 宁安市| 女性| 嘉黎县| 阜新市| 阳东县| 杭锦旗| 石渠县| 寻甸| 永安市| 阳东县| 历史| 安新县| 青河县| 福清市| 旬邑县| 洞头县| 永泰县| 苏尼特左旗| 云梦县| 青龙| 启东市| 德兴市| 瓦房店市| 雷山县| 娱乐| 永清县| 托里县| 桂阳县|