简体   繁体   English

使用过滤器渲染 html

[英]Render html using filter

I need to write a filter which renders html tags.我需要编写一个呈现 html 标签的过滤器。 This is my filter:这是我的过滤器:

filters: {
  limitStrLength: function (value, maxLength) {
    if (value && value.length > maxLength) {
      let partialVal = value.substr(0, maxLength) + "...";
      return "<span title='" + value + "' >" + partialVal + "</span>";
    } else {
      return value;
    }
  }
}

My problem is that I do not manage to render the raw html.我的问题是我无法呈现原始 html。

If I simply do:如果我只是这样做:

<div>{{ productName  | limitStrLength(6) }}</div>

The html tags are rendered as string (ie, I see on screen something like <span title=... ). html 标签呈现为字符串(即,我在屏幕上看到类似<span title=... )。

I also tried using v-html attribute:我也尝试使用v-html属性:

<div v-html="productName | limitStrLength(6)"></div>

But I get an error:但我收到一个错误:

Property or method "limitStrLength" is not defined on the instance but referenced during render.属性或方法“limitStrLength”未在实例上定义,但在渲染期间被引用。

Any idea?任何的想法?

Try this:尝试这个:

<div v-html="$options.filters.limitStrLength(productName, 6)"></div>

Source: Github issue来源: Github 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM