- 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>
- GAE編程指南
- ThinkPHP 5實戰
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- 神經網絡編程實戰:Java語言實現(原書第2版)
- Learn WebAssembly
- Kinect for Windows SDK Programming Guide
- 從0到1:Python數據分析
- Learning Hunk
- MySQL從入門到精通(軟件開發視頻大講堂)
- Python深度學習原理、算法與案例
- Learning Modular Java Programming
- 從Power BI到Analysis Services:企業級數據分析實戰
- jQuery從入門到精通(微課精編版)
- 金融商業數據分析:基于Python和SAS
- Professional JavaScript