SoFunction
Updated on 2025-04-04

vue directive v-html uses filters function example

question

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

Vue2.0 no longer supports the use of filters in v-html, for example, in 1.0:

{{{ | highlight }}}

However, it is no longer available, and the Vue2.0 filters can now only be used in {{ }} and v-bind.

However, what should I do if I feel it is troublesome and want to use it?

Solution

  • Using global methods
  • Use the computed property
  • Use $

Using global methods

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

Global methods can be defined on Vue:

= function (sTitle) {
 // to do
};

Then you can use this method directly on all components:

v-html="highlight()"

Use the computed property

  • 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.

Of course, you can use the computed attribute to return the native html to v-html.

Use $

You can use $

v-html="$()".

This method is not explained in the documentation, but it is also a reliable method.

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

The above is all the knowledge points introduced about the use of vue directive v-html this time. Thank you for your reading and support for me.