vue指令v-html使用過濾器filters功能實例
問題
2.0 filters only work in mustache tags and v-bind.
Vue2.0 不再支持在 v-html 中使用過濾器,比如在 1.0 中是這樣使用的:
{{{ option.title | highlight }}}
然而,現(xiàn)在不能使用了,Vue2.0 的過濾器現(xiàn)在只能應(yīng)用在 {{ }} 和 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.
當(dāng)然,可以使用計算屬性 computed,返回原生 html 給 v-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
以上就是本次介紹的關(guān)于vue指令v-html使用的全部知識點(diǎn),感謝大家的閱讀和對腳本之家的支持。
相關(guān)文章
解決element ui el-row el-col里面高度不一致問題
這篇文章主要介紹了解決element ui el-row el-col里面高度不一致問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08vue實現(xiàn)標(biāo)簽頁切換/制作tab組件詳細(xì)教程
在項目開發(fā)中需要使用vue實現(xiàn)tab頁簽切換功能,所以這里總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)標(biāo)簽頁切換/制作tab組件的相關(guān)資料,需要的朋友可以參考下2023-11-11vue分片上傳視頻并轉(zhuǎn)換為m3u8文件播放的實現(xiàn)示例
前端上傳大文件、視頻的時候會出現(xiàn)超時、過大、很慢等情況,為了解決這一問題,跟后端配合做了一個切片的功能,本文主要介紹了vue分片上傳視頻并轉(zhuǎn)換為m3u8文件播放的實現(xiàn)示例,感興趣的可以了解一下2023-11-11