<!-- 在雙花括號中 --> {{ message | capitalize }} <!-- 在 `v-bind` 中 --> <div v-bind:id="rawId | formatId"></div>
methods: {...}, filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } }, watch: {...}
Vue.filter('capitalize', function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) }) new Vue({ // ... })
// 第一個參數都是默認傳入的值,第二開始都是手動傳入的參數 const test1 = val => { return val * 2 } const test2 = (val, num = 0) => { return val * num } export { test1, test2 }
import * as filters from './api/filters'
Object.keys(filters).forEach(key => Vue.filter(key, filters[key]))
<template> <div> <h1>測試filters</h1> <p>{{ num | test1 }}</p> <p>{{ num | test2 }}</p> <p>{{ num | test2(3) }}</p> <div v-bind:id="num | test2(4)">111111111</div> </div> </template> <script> export default { data () { return { num: 2 } } } </script>