轉載於簡書
連接:http://www.jianshu.com/p/29b7eaabd1ba
2.0 filters only work in mustache tags and v-bind.javascript
Vue2.0 再也不支持在 v-html
中使用過濾器,好比在 1.0 中是這樣使用的:html
{{{ option.title | highlight }}}
然而,如今不能使用了,Vue2.0 的過濾器如今只能應用在 {{ }}
和 v-bind
中。
然而,嫌麻煩,還想使用怎麼辦?java
put your
highlight
into methods, andv-html="highlight(option.title)"
spa
能夠在 Vue 上定義全局方法:prototype
Vue.prototype.highlight= function (sTitle) { // to do };
而後全部組件上均可以直接用這個方法了:code
v-html="highlight(option.title)"
- What if I have a filter that outputs HTML? Do I have to use a computed property or is there a better way?
- Computed properties are the best way. You get automatic caching.
固然,能夠使用計算屬性 computed
,返回原生 html
給 v-html
便可。component
You can use $options.filtershtm
v-html="$options.filters.highlight(option.title)".
這個方式在文檔中並無說明,可是這也是可靠的方法。ip
You can safely rely on that: $options are the options passed to the Vue constructor when creating a vm (so any component or new Vue). From that point on is just javascript文檔