- 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>
- DBA攻堅指南:左手Oracle,右手MySQL
- LabVIEW Graphical Programming Cookbook
- Visual Basic程序設計教程
- 概率成形編碼調制技術理論及應用
- 計算機應用基礎教程(Windows 7+Office 2010)
- Creating Data Stories with Tableau Public
- .NET 4.5 Parallel Extensions Cookbook
- Python Web自動化測試設計與實現
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Java程序設計入門(第2版)
- Getting Started with RethinkDB
- Java EE應用開發及實訓
- Mastering Swift 4(Fourth Edition)
- C++教程