我正在react-native webview 中渲染ZingChart。我需要使用 %data-day
访问 zingchart 的 token ,但每当我尝试使用 %d
时,它都会打印 NaN
下面是示例代码:
import React, { Component } from 'react';
import { Text, View, WebView } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
}
renderWeb = () => {
return `
<html>
<head>
</head>
<body>
<script>
${console.log('%d', ':data')}
</script>
</body>
</html>
`;
};
render() {
return (
<View style={{ flex: 1 }}>
<WebView source={{ html: this.renderWeb() }} />
</View>
);
}
}
谁能帮我解决这个问题吗?
最佳答案
console.log 的第一个参数被视为格式字符串,其中百分号字符引入替换序列。它们大致基于 C 中 printf
函数使用的格式字符串。请参阅 MDN 中的“使用字符串替换”console article 。
%d
使用 console.log
调用的下一个未使用的参数并将其格式化为数字。由于 ':data'
不是数字,Chrome 在浏览器中测试时会将其转换为日志中的字符串“NAN”。然而,它依赖于浏览器 - Firefox 将其转换为零。
要记录字符串“%d”,请在格式字符串中将百分号加倍并编码为“%%d”。就像 C 语言一样:-)
关于javascript - 包含 '%d' 的字符串正在转换为 'NaN',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540919/