在 div 中显示来自 JSON 的内容

Display content from JSON inside a div

我是html、javascript和vue的新手。我不确定这是 vue 特定的还是可以使用一些 javascript 魔法来解决。

我有基于 NodeJS 的服务,其中 UI 用 VueJS 编写。该页面的内容来自 markdown 编辑器,nodejs 使用 showdown 将其转换为 html。 Nodejs 的响应是 json,我正在尝试使用 Vue 在屏幕上显示它,如下所示

 app.js (vue)

 new Vue({
   el: "#mdEditor",

   data: {
      content: '',
    },
   mounted: function() {
     this.defaultPage();
   },

  methods: {
    defaultPage: function() {
      this.$http.get('/defaultPage')
       .then((result) => {
          this.$set(this, 'content', result.data.html);
          console.log('result=', result);
         }, (err) => {
           console.log('error=', err);
       });
   }
 }
});

HTML 文件

  <div class="container" id="mdEditor">
<div class="col-sm-12">
  <div class="panel panel-primary">
    <div class="panel-body">
      {{content}}
    <!-- content of the md file goes here-->
    </div>
  </div>
</div>

但是内容(即 html 代码)打印为文本而不是 html。 预先感谢您的帮助

使用 v-html 属性渲染原始 html。在 docs here.

中阅读更多内容

请务必注意,像这样插入原始 html 可能是一个安全漏洞(请参阅文档中的注释)。