javascript - Render html from string vuejs - Stack Overflow

I'm struggling with a problem, I want to create a chat app, and the users to be able to tag each o

I'm struggling with a problem, I want to create a chat app, and the users to be able to tag each other, by using @. For example, user1 type in chat input 'hi @user2', the output should look the same, but '@user2' be a link instead of simple text, I figured how to do that, but the problem is that, if user input for example html tags, it's rendered as html.

HTML:

<span v-html="chatFormat(chat.text)"></span>

VUE:

chatFormat(text) {
  var words = text.split(" ");
  var newStr = '';
  words.forEach(function (word) {
  if (word.includes('@') >= 1) {
    if (word.charAt(0) == '@') {
      word = '{{ <a href="/user" class="font-weight-bold" style="text-decoration:none">' + word + '</a> }}';
    }
  }
  newStr = newStr + ' ' + word;
  });
  return newStr;
}

I'm struggling with a problem, I want to create a chat app, and the users to be able to tag each other, by using @. For example, user1 type in chat input 'hi @user2', the output should look the same, but '@user2' be a link instead of simple text, I figured how to do that, but the problem is that, if user input for example html tags, it's rendered as html.

HTML:

<span v-html="chatFormat(chat.text)"></span>

VUE:

chatFormat(text) {
  var words = text.split(" ");
  var newStr = '';
  words.forEach(function (word) {
  if (word.includes('@') >= 1) {
    if (word.charAt(0) == '@') {
      word = '{{ <a href="/user" class="font-weight-bold" style="text-decoration:none">' + word + '</a> }}';
    }
  }
  newStr = newStr + ' ' + word;
  });
  return newStr;
}
Share Improve this question edited Oct 30, 2020 at 0:37 PLASMA chicken 2,7852 gold badges17 silver badges28 bronze badges asked Oct 16, 2020 at 6:52 Pricop George-TiberiuPricop George-Tiberiu 353 silver badges8 bronze badges 2
  • stackoverflow./a/48226843/381282 – Michal Levý Commented Oct 16, 2020 at 7:16
  • Does this answer your question? Rendering html tags in Vue.js – Sébastien M. Commented Oct 16, 2020 at 7:25
Add a ment  | 

2 Answers 2

Reset to default 4

Well, using v-html directive renders value as HTML - it's not parsed for variables. You could change Your code and get rid of {{}} so only link would appear in the output.

word = '<a href="/user" class="font-weight-bold" style="text-decoration:none">' + word + '</a>';

But, be aware that using v-html to display user-input is a risky thing. User input should be escaped first to get rid of potential harmful code. (You can check Sanitizing user input before adding it to the DOM in Javascript as Michal Levý suggested)

I would also suggest using arrow functions and template literals

chatFormat(text) {
  // using let instead of var for declaring variables in the local scope
  let safeText= this.sanitizeHTML(text), // for security reasons
      words = safeText.split(" "),
      newStr = '';

  words.forEach(word => { // arrow function
    if (word.includes('@') >= 1) {
      if (word.charAt(0) == '@') {
        word = `<a href="/user" class="font-weight-bold" style="text-decoration:none">${word}</a>`; // template literal
      }
    }
    newStr = newStr + ' ' + word;
  });

  return newStr;
},

// This should be a secure sanitization method from some library
sanitizeHTML(string) {
  return string
},

A subject that has been discussed an number of times.

in Vuejs: https://v2.vuejs/v2/api/#v-html

in stackoverflow.: (one of many) Rendering html tags in Vue.js

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745151613a4613895.html

相关推荐

  • javascript - Render html from string vuejs - Stack Overflow

    I'm struggling with a problem, I want to create a chat app, and the users to be able to tag each o

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信