比如说,我在 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
函数,但是没有地方可以查看用于生成它的字符串。
但是,如果您使用字符串来指定您的模板,this.$options.template
将包含该字符串。
关于javascript - 在 Vue 中获取模板作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48267129/