0
我有一个过滤器可以将带有id的内容转换为用户名。例如,它将Thank you @id-3124324 !
转换为Thank you @Jack !
。Vue.js过滤器中的异步数据
var filter = function (content) {
var re = /\[email protected](id\-\d+)\s/g;
var matches = [];
var lastMatch = re.exec(content);
while (lastMatch !== null) {
matches.push(lastMatch[1]); // uid being mentioned
lastMatch = re.exec(content);
}
// TODO: query user name from matched id
// replace id with user name
// fake usernames here
var usernames = ['Random Name'];
for (var i = 0; i < usernames.length; ++i) {
content = content.replace(new RegExp(matches[i], 'g'), usernames[i]);
}
return content;
};
Vue.filter('username', filter);
但在我的情况下,usernames
应该通过带有id的查询的AJAX来实现。我应该怎么做?
只是做一个AJAX调用您的API或什么,当你得到的结果,从你的Ajax响应 –
@PrimozRome返回内容那么如何更新内容? – Ovilia
不知道你的应用程序如何工作,但我不会用过滤器做到这一点......如果你有ID,只需使用Vue处理ajax调用你的API以返回该ID的用户名。 –