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

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>
主站蜘蛛池模板: 顺义区| 乐陵市| 科技| 中宁县| 五河县| 保靖县| 仁寿县| 临夏市| 崇信县| 高阳县| 秀山| 雷山县| 巴南区| 兴安县| 新兴县| 昭平县| 临汾市| 昭平县| 东城区| 盐源县| 五台县| 栾城县| 巨鹿县| 将乐县| 秦安县| 湘潭县| 夏津县| 庐江县| 孝感市| 卢湾区| 伊金霍洛旗| 墨江| 扎囊县| 平原县| 会宁县| 马山县| 黄大仙区| 哈巴河县| 大渡口区| 新晃| 泽州县|