如何实现Vue指令v-html结合过滤器filters的实例应用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计493个文字,预计阅读时间需要2分钟。
问题:Vue 2.0 中过滤器在哪些情况下可用?
Vue 2.0 过滤器现在仅在工作标签和 v-bind 中可用。Vue 2.0 不再支持在 v- 中使用过滤器,例如在 1.0 中可以这样使用:{{ option.title | highlight }}。但现在无法使用。
问题
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,返回原生 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
以上就是本次介绍的关于vue指令v-html使用的全部知识点,感谢大家的阅读和对易盾网络的支持。
本文共计493个文字,预计阅读时间需要2分钟。
问题:Vue 2.0 中过滤器在哪些情况下可用?
Vue 2.0 过滤器现在仅在工作标签和 v-bind 中可用。Vue 2.0 不再支持在 v- 中使用过滤器,例如在 1.0 中可以这样使用:{{ option.title | highlight }}。但现在无法使用。
问题
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,返回原生 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
以上就是本次介绍的关于vue指令v-html使用的全部知识点,感谢大家的阅读和对易盾网络的支持。

