javascript - How can I display rich text content in vue.js? - Stack Overflow

I'm creating a web application and trying to display uploaded rich text content by using vue.js. T

I'm creating a web application and trying to display uploaded rich text content by using vue.js. The rich text content is created by Action Text, and the format is as shown below.

<h1>text</h1><div>sample text<br><action-text-attachment sgid="BAh7CEkiCGdpZAY6BkVUSSI3Z2lkOi8vZWxvb3AtcmljaC9BY3RpdmVTdG9yYWdlOjpCbG9iLzEzP2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--36dd18a0ed30ace4bc8442849d9dd3355bc86443" content-type="image/png" url="http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e887e47b082a537f647284bc0bfb6b6216f51216/deal.png" filename="deal.png" filesize="10394" width="256" height="256" presentation="gallery"></action-text-attachment></div>

I tried to display the content like this in vue.js file.

<div v-html=""></div>

Then, texts are properly displayed, but images are not displayed. How can I solve it?

I'm creating a web application and trying to display uploaded rich text content by using vue.js. The rich text content is created by Action Text, and the format is as shown below.

<h1>text</h1><div>sample text<br><action-text-attachment sgid="BAh7CEkiCGdpZAY6BkVUSSI3Z2lkOi8vZWxvb3AtcmljaC9BY3RpdmVTdG9yYWdlOjpCbG9iLzEzP2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--36dd18a0ed30ace4bc8442849d9dd3355bc86443" content-type="image/png" url="http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e887e47b082a537f647284bc0bfb6b6216f51216/deal.png" filename="deal.png" filesize="10394" width="256" height="256" presentation="gallery"></action-text-attachment></div>

I tried to display the content like this in vue.js file.

<div v-html=""></div>

Then, texts are properly displayed, but images are not displayed. How can I solve it?

Share Improve this question edited Oct 11, 2019 at 15:08 Misaki Agata asked Oct 11, 2019 at 12:09 Misaki AgataMisaki Agata 231 gold badge1 silver badge4 bronze badges 4
  • <div v-html=""></div> displays nothing. – Abdelillah Aissani Commented Oct 11, 2019 at 12:15
  • So are you just asking how to convert an <action-text-attachment> to an <img>? Is the url attribute pointing at the right place for that image? – skirtle Commented Oct 11, 2019 at 12:27
  • @Dadboz I'm sorry for my bad writing, I just wanted to point that I used this directive. – Misaki Agata Commented Oct 11, 2019 at 12:51
  • @skirtle That's true, I want to convert an <action-text-attachment> to an <img>. The url is correct. – Misaki Agata Commented Oct 11, 2019 at 12:53
Add a ment  | 

1 Answer 1

Reset to default 2

I'm not familiar with the rich text format created by Action Text so I can't ment on whether using it in conjunction with v-html is safe.

One approach would be to manipulate the text prior to inserting it. If the Action Text format is very predictable then this wouldn't be difficult using a RegExp. Trying to parse HTML more generally is a bit tricky.

In the example below I've gone with a different approach. This inserts the text unchanged using v-html and then updates the DOM to insert <img> elements in place of <action-text-attachment> elements. It only sets the src attribute but you could set other attributes as you deem appropriate.

new Vue({
  el: '#app',
  
  data () {
    return {
      text: '<h1>text</h1><div>sample text<br><action-text-attachment sgid="BAh7CEkiCGdpZAY6BkVUSSI3Z2lkOi8vZWxvb3AtcmljaC9BY3RpdmVTdG9yYWdlOjpCbG9iLzEzP2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--36dd18a0ed30ace4bc8442849d9dd3355bc86443" content-type="image/png" url="https://vuejs/images/logo.png" filename="deal.png" filesize="10394" width="256" height="256" presentation="gallery"></action-text-attachment></div>'
    }
  },
  
  mounted () {
    this.updateImages()
  },
  
  updated () {
    this.updateImages()
  },
  
  methods: {
    updateImages () {
      const attachments = this.$el.querySelectorAll('action-text-attachment[content-type="image/png"]')
      
      for (const attachment of attachments) {
        const img = document.createElement('img')
        img.setAttribute('src', attachment.getAttribute('url'))
        attachment.parentNode.replaceChild(img, attachment)
      }
    }
  }
})
<script src="https://unpkg./[email protected]/dist/vue.js"></script>

<div id="app">
  <div v-html="text"></div>
</div>

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信