【发布时间】:2017-06-01 00:43:13
【问题描述】:
如何使用过滤器显示原始 html?
我有这样的事情:
K.json = function( json ) {
if( typeof json!='string' ) json = JSON.stringify( json, null, 2 );
json = json.replace( /</g, '<' ).replace( />/g, '>' ); // replace(/&/g, '&')
var pattern = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g;
var html = json.replace( pattern, function( match ) {
var cls = 'number';
var suffix = '';
if( /^"/.test( match ) ) {
if( /:$/.test( match ) ) {
cls = 'key';
match = match.slice( 0, -1 );
suffix = ':'
} else {
cls = 'string';
}
} else if( /true|false/.test( match ) ) {
cls = 'boolean';
} else if( /null/.test( match ) ) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>' + suffix;
} );
return html;
};
Vue.filter( 'json', K.json );
并像这样使用它们:
<div v-html="thecolumn | json"></div>
显示警告并显示不正确:
vue.js:523 [Vue warn]: Property or method "json" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
(在根实例中找到)
我也尝试了来自论坛的解决方案:https://laracasts.com/discuss/channels/vue/use-a-filter-custom-filter-in-v-html-property?page=1
<p v-html="this.$options.filters.json(description)"></p>
显示错误:
[Vue warn]: Error when rendering root instance:
vue.js:3063 Uncaught TypeError: Cannot read property 'filters' of undefined
at eval (eval at makeFunction (vue.js:8260), <anonymous>:2:2975)
at Proxy.renderList (vue.js:3158)
at Proxy.eval (eval at makeFunction (vue.js:8260), <anonymous>:2:2169)
at Vue$3.Vue._render (vue.js:3054)
at Vue$3.<anonymous> (vue.js:2430)
at Watcher.get (vue.js:1661)
at new Watcher (vue.js:1653)
at Vue$3.Vue._mount (vue.js:2429)
at Vue$3.$mount (vue.js:6000)
at Vue$3.$mount (vue.js:8327)
在 VueJS2 上执行此操作的正确方法是什么?
【问题讨论】:
-
我认为没有 - 在
v-html中,所有数据绑定等都被忽略了。
标签: vuejs2