vue.jsvuejs2vue-filter

vue-filter doesn't work with v-html


This problem has few solutions but nothing worked for me. I am using a package called vue-filter and I want to use nl2br filter. When I try to use it in my html like this

<p style="padding: 10px;font-size: 17px;"  v-html="$options.filters.nl2br(opDesc.description)"></p>

It gives me the following error

TypeError: _vm.$options.filters.nl2br is not a function

Here is the package github link https://github.com/wy-ei/vue-filter#nl2br


Solution

  • Its ok. Filters does not works in v-html directive. Only in mustache interpolations and v-bind directive.

    Use computed property instead:

    <any-tag v-html="filtered" />
    

    In component, use something like this:

    computed: {
      filtered () {
        return this.nl2br(this.rawHtml)
      }
    },
    methods: {
      nl2br (source) {
        var filtered = //Filter logic here
        return filtered
      }
    }