0

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:

<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>
2
  • .then(err => console.log(err)) will always log undefined, because the previous then() returns undefined. The result of an assignment is undefined, regardless of what you're assigning. You probably meant .catch(err => console.log(err)). Commented Mar 10, 2022 at 20:28
  • thanks! yes I meant .catch here Commented Mar 10, 2022 at 20:40

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It works! Didn't think its so simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.