zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Kubernetes日志的6个最佳实践
    如何选出适合自己的管理Helm Chart的最佳方式?
    授权权限服务设计解析
    微服务中如何设计一个权限授权服务
    微服务中的网关
    ketchup服务治理
    ketchup 消息队列rabbitmq使用
    ketchup 注册中心consul使用
    微服务框架 ketchup 介绍
    微服务框架surging学习之路——序列化
  • 原文地址:https://www.cnblogs.com/lanshengzhong/p/7987693.html
Copyright © 2011-2022 走看看