首页 > 解决方案 > 在 Vue 中解析 v-html 内容

问题描述

我有一个页面,我目前正在使用 Wordpress 的 REST api 获取 html 字符串。我希望能够创建一个导航栏组件来解析 html,查看所有标题标签并为每个标题生成锚链接。是否可以使用Vue搜索v-html来动态创建这个组件?

这是获取 wordpress 数据的 axios 调用。我得到的终点是从一个帖子页面返回“内容”——这是一个我正在使用 v-html 呈现的 HTML 字符串。

data() {
    return {
        loading: true,
        baseUrl: 'https://sitename.co.uk/wp-json/wp/v2/posts/'
    }
},

methods: {
    fetchData() {
        axios.get(`${this.baseUrl}${this.$route.params.id}/`)
            .then((resp) => {
                const data = resp.data; //this is the wp data
            })
            .catch((err) => {
                console.log(err);
            })
    }
}

标签: wordpressvue.jsheadless

解决方案


据我所知,您无法在 v-html 中导入 head 标签,因此,您可以在 vue js 中使用 _slug 将组件呈现为静态页面。


推荐阅读