- Vue.js 2.x by Example
- Mike Street
- 185字
- 2021-07-02 20:00:31
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>
- Learning Java Functional Programming
- Learning ArcGIS Pro 2
- 算法基礎:打開程序設計之門
- INSTANT MinGW Starter
- Java開發入行真功夫
- OpenNI Cookbook
- Flash CS6中文版應用教程(第三版)
- 深入淺出PostgreSQL
- FPGA嵌入式項目開發實戰
- Python商務數據分析(微課版)
- Python大規模機器學習
- Learning Grunt
- 數據科學中的實用統計學(第2版)
- Mastering PowerCLI
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)