如何在Vue JS中渲染服务器发送的HTML文件?

我对 Vue JS 很陌生。目前,我正在寻找一种呈现服务器发送的 HTML 页面的方法。假设我有一个节点路由器“health”,它发送如下 HTML 文件。


res.sendFile('test.html', { root: path.dirname(require.main.filename) + '/public/' });

在这种情况下,我可以尝试访问该文件并通过 Vue JS 以这种方式呈现它吗?


<div id="app">

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

</div>

<script>

let sessionId = "";

(function() {

    const app = new Vue({

        el: '#app',

        data: {

            reactPage: undefined

        },

        created() {

            fetch('launch/health')

                .then(response => {

                    this.reactPage = response;

                })

        }

    })

})();

这段代码没有按我的预期工作。


阿晨1998
浏览 37回答 1
1回答

白衣非少年

是的,这里唯一的问题是fetch语法。尝试这个:fetch('launch/health').then(response => {&nbsp; return response.text();}).then(data => {&nbsp; this.reactPage = data;})您可以考虑使用axios更简单的 http 请求。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5