uni-app中js-beautify格式化type="text/html"的script标签报错

uni-app中js-beautify格式化type="text/html"的script标签报错

格式化这样的标签就报错

<script type="text/html" template>  

</script>

2024-11-20 09:47:01.040 [WARNING:] [PluginHost] [Beautify] Error: Invalid Option Value: The option ‘wrap_attributes’ can contain only the following values: auto,force,force-aligned,force-expand-multiline,aligned-multiple,preserve,preserve-aligned You passed in: ‘false’ at e._get_selection_list (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:5021) at e._get_selection (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:4541) at new s (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:44261) at new f (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:31949) at s (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:36317) at f._print_custom_beatifier_text (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:36963) at f._handle_text (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:35809) at f.beautify (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:33979) at t.exports (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:43718) at Object.a [as html] (D:\Program Files\HBuilderX.4.07.2024032720\HBuilderX\plugins\format\out\index.js:1:50881)


更多关于uni-app中js-beautify格式化type="text/html"的script标签报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app中js-beautify格式化type="text/html"的script标签报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中,当你尝试使用js-beautify库来格式化包含type="text/html"<script>标签内的内容时,可能会遇到报错。这通常是因为js-beautify默认处理的是JavaScript代码,而不是HTML内容。为了正确格式化这些HTML内容,你需要对HTML部分进行专门的处理。

以下是一个示例代码,展示了如何在uni-app中针对type="text/html"<script>标签内容进行HTML格式化。我们将使用html-beautify函数(假设你已经通过npm安装了js-beautify库)。

首先,确保你已经安装了js-beautify库:

npm install js-beautify --save-dev

然后,在你的uni-app项目中,你可以创建一个函数来专门处理这些HTML内容的格式化。以下是一个示例代码:

// 引入js-beautify库中的html-beautify函数
const beautify = require('js-beautify').html;

// 示例HTML内容,通常你会从<script type="text/html">标签中提取这部分内容
const htmlContent = `
<div><p>Hello, World!</p><ul><li>Item 1</li><li>Item 2</li></ul></div>
`;

// 配置html-beautify的选项
const options = {
    indent_size: 2, // 缩进大小
    indent_char: ' ', // 缩进字符
    max_preserve_newlines: 1, // 最大保留的新行数
    preserve_newlines: true, // 是否保留换行
    keep_array_indentation: false, // 是否保持数组的缩进
    break_chained_methods: false, // 是否在链式调用后换行
    indent_scripts: 'normal', // 脚本缩进
    brace_style: 'collapse', // 大括号风格
    space_before_conditional: true, // 条件语句前是否空格
    unescape_strings: false, // 是否解码字符串中的HTML实体
    jslint_happy: false, // 是否输出jslint兼容格式
    wrap_line_length: 0, // 每行最大字符数(0表示不限制)
    indent_inner_html: true, // 是否缩进HTML标签内部的内容
    comma_first: false, // 是否在对象字面量的属性前添加逗号
    e4x: false, // 是否启用E4X语法
    indent_empty_lines: false // 是否缩进空行
};

// 使用html-beautify函数格式化HTML内容
const beautifiedHtml = beautify(htmlContent, options);

// 输出格式化后的HTML内容
console.log(beautifiedHtml);

这段代码展示了如何使用js-beautifyhtml-beautify函数来格式化HTML内容。你需要将实际的HTML内容从<script type="text/html">标签中提取出来,然后传递给beautify函数。记得根据你的具体需求调整options对象中的配置。

通过这种方式,你可以避免在uni-app中直接对type="text/html"<script>标签使用js-beautify进行JavaScript格式化而导致的错误。

回到顶部