javascript - 在 Vue 中获取模板作为字符串

标签 javascript vue.js

比如说,我在 Vue 中有以下单个文件组件:

// Article.vue

<template>
  <div>
    <h1>{{title}}</h1>
    <p>{{body}}</p>
  </div>
</template>

在另一个文件中导入该组件后,是否可以将其模板作为字符串获取?

import Article from './Article.vue'

const templateString = // Get the template-string of `Article` here.

现在 templateString 应该包含:

<div>
  <h1>{{title}}</h1>
  <p>{{body}}</p>
</div>

最佳答案

这是不可能的。

Under the hood, Vue compiles the templates into Virtual DOM render functions.

所以你的编译组件将有一个render函数,但是没有地方可以查看用于生成它的字符串。

Vue is not a string-based templating engine

但是,如果您使用字符串来指定您的模板,this.$options.template 将包含该字符串。

关于javascript - 在 Vue 中获取模板作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48267129/

相关文章:

css - vuetify 上有卡片页脚/卡片组功能吗?

vue.js - 使用 Vue 读取动态生成的输入 radio 的值

javascript - 一次性解析 promise 单例(Angular 服务)

javascript - 与nodejs的数据和字符串比较

javascript - Toastr 抛出类型错误 toastr.info ('msg' )

javascript - 已经设置后,如何将 React Hooks 设置为另一个值

javascript - 使用 strict 会使 jshint 无法使用

javascript - Response 的数据部分是一个长脚本而不是所需的 json 对象

javascript - 如何使用 VueJS 动态添加属性?

javascript - 组件名称无效 : "pages/product/_slug.vue". 组件名称应符合 html5 规范中有效的自定义元素名称