Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing the JSON from API with HTML in the Vue.js app

I have a simple Vite+Vue.js project in which I am importing data from headless-cms WordPress using REST API and JSON. It should take and display titles and content of the posts (including imgs when they occure). I’m stuck because all the data on the page display like HTML, i.e. it contains HTML elements, but of course are JSON. Is there any way to convert it to plain HTML? I’ve tried filtering it with "replacer" method, but for all elements and situations it would take ages.

Screenshot of how data display on page

My component template:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
<h1>Posts</h1>
<div v-for="post in posts" :key="post.id" class="posts">
    <h2> {{ post.title.rendered }} </h2>
    <div> {{ post.content.rendered }} </div>
</div>

My script in that component:

<script>
export default {
    data() {
        return {
            posts: [],
            message: String,
        }
    },
    mounted() {
        fetch('https://my-url-here.com/wp-json/wp/v2/posts')
        .then(res => res.json())
        .then(data => this.posts = data)
        .then(err => console.log(err)) 
    }
}

</script>

>Solution :

Just use v-html

<template>
<h1>Posts</h1>
<div v-for="post in posts" :key="post.id" class="posts">
    <h2> {{ post.title.rendered }} </h2>
    <div v-html="post.content.rendered"></div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading