日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

vue项目中 指令 v-html 中使用过滤器filters功能

發布時間:2023/12/18 编程问答 44 如意码农
生活随笔 收集整理的這篇文章主要介紹了 vue项目中 指令 v-html 中使用过滤器filters功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載于簡書
鏈接:http://www.jianshu.com/p/29b7eaabd1ba

問題

2.0 filters only work in mustache tags and v-bind.

Vue2.0 不再支持在 v-html 中使用過濾器,比如在 1.0 中是這樣使用的:

{{{ option.title | highlight }}}

然而,現在不能使用了,Vue2.0 的過濾器現在只能應用在 {{ }}v-bind 中。
然而,嫌麻煩,還想使用怎么辦?

解決方法

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

使用全局方法

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

可以在 Vue 上定義全局方法:

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

然后所有組件上都可以直接用這個方法了:

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 即可。

使用 $options.filters

You can use $options.filters

v-html="$options.filters.highlight(option.title)".

這個方式在文檔中并沒有說明,但是這也是可靠的方法。

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

總結

以上是生活随笔為你收集整理的vue项目中 指令 v-html 中使用过滤器filters功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。