[轉]vue項目中 指令 v-html 中使用過濾器filters功能

轉載於簡書
連接: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

解決方法

  • 使用全局方法
  • 使用 computed 屬性
  • 使用 $options.filters

使用全局方法

put your highlight into methods, and v-html="highlight(option.title)"spa

能夠在 Vue 上定義全局方法:prototype

Vue.prototype.highlight= function (sTitle) { // to do };

而後全部組件上均可以直接用這個方法了:code

v-html="highlight(option.title)"

使用 computed 屬性

  • 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,返回原生 htmlv-html 便可。component

使用 $options.filters

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文檔

相關文章
相關標籤/搜索