我正在构建一个 React 应用程序,我需要根据从 API 获取的数据动态更新文档标题和图标。具体来说,我想使用 API 响应中的
brandName
字段来设置文档标题和图标。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" **href={profileImage}**/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>**{brandName}**</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
从API获取数据后直接改变
document
对象怎么样?
const data = await fetchMyData();
document.title = data.brandName;
const faviconElement = document.querySelector("link[rel='icon']");
if (faviconElement) {
faviconElement.href = data.profileImage;
}