我有一個簡單的Vite Vue.js專案,我在其中使用 REST API 和 JSON 從 headless-cms Wordpress 匯入資料。它應該獲取并顯示帖子的標題和內容(包括出現時的 imgs)。我被卡住了,因為頁面上的所有資料都像 HTML 一樣顯示,即它包含 HTML 元素,但當然是 JSON。有沒有辦法將它轉換為純 HTML?我試過用“替換器”方法過濾它,但對于所有元素和情況,它需要很長時間。
資料如何在頁面上顯示的螢屏截圖
我的組件模板:
<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>
我在該組件中的腳本:
<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>
uj5u.com熱心網友回復:
只需使用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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442954.html
標籤:javascript html json Vue.js 无头cms
上一篇:VueJS字串化路由
下一篇:如何被動地設定一個<html>類
