在javascript文件中注释HTML代码



我正在react中进行开发,但我认为这个问题适用于任何混合javascript和HTML的语言。

我想知道是否有一种简单的方法可以在js文件中注释HTML代码

例如,在返回函数中,我通常有这样的东西:

return (
<View style={styles.container}>
<Text style={styles.text}>Text 1</Text>
<Text style={styles.text}>Text 2</Text>
<Text style={styles.text}>Text 3</Text>
</View>
);

如果我能评论Text2,我会非常有用我尝试使用HTML注释<!-- -->,但它抱怨!是一个意外的标记。

Failed building JavaScript bundle.
SyntaxError: /home/isaac/Projects/AwesomeProject2/App.js: Unexpected token (20:5

在javascript文件中注释HTML代码的最佳方式是什么?

使用{/* ... */}对JSX代码进行注释
此外,大多数编辑器都支持Ctrl + /快捷方式,以便用适当的语法注释掉代码。

更多信息:https://wesbos.com/react-jsx-comments

为了在.jsx文件中注释一行,您需要这样做:

return (
<View style={styles.container}>
{/* <Text style={styles.text}>Text 1</Text> */}
<Text style={styles.text}>Text 2</Text>
<Text style={styles.text}>Text 3</Text>
</View>
);

最新更新