如何生成template? #8253
Unanswered
ZhangHang12135
asked this question in
Help/Questions
如何生成template?
#8253
Replies: 2 comments 2 replies
-
这里是 我的代码 import { baseParse, transform, generate, NodeTransform, NodeTypes, getBaseTransformPreset } from '@vue/compiler-core';
const template = `<template>
<div>hah</div>
</template>`;
// 将模板字符串解析为 AST
const ast = baseParse(template);
const plugin: NodeTransform = (node, context) => {
if (node.type === NodeTypes.ELEMENT && node.tag === 'div') {
context.replaceNode(Object.assign({}, node, {
tag: 'p',
children: [
{
type: NodeTypes.TEXT,
content: 'hello',
isEmpty: false
}
]
}));
}
}
const [nodeTransforms] = getBaseTransformPreset(true);
transform(ast, {
nodeTransforms: [...nodeTransforms, plugin]
})
const code = generate(ast, {
mode: 'module',
});
console.log(code); |
Beta Was this translation helpful? Give feedback.
0 replies
-
我去试试用HTML parser 的解析器, https://github.com/rehypejs/rehype |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
使用@vue/compile-core, 我将一个template 代码,用baseParse 编译成AST ,然后用transform 做一些修改,我希望生成修改后的代码,我应该怎么做。我发现 generate生成的是render 函数,但是我想要的是可编辑的源代码
Beta Was this translation helpful? Give feedback.
All reactions