gpt4 book ai didi

string - typescript - 逗号运算符的左侧未使用且没有副作用 - 如何在 hookrouter 的路由中使用常量代替字符串?

转载 作者:行者123 更新时间:2023-12-04 15:25:21 31 4
gpt4 key购买 nike

import React from "react";
import Users from "./components/Users";
import Contact from "./components/Contact";
import About from "./components/About";
const routes = {
"/": () => <Users />,
"/about": () => <About />,
"/contact": () => <Contact />
};
export default routes;

我可以知道如何在路由中使用常量而不是字符串,如下所示?
const root = "/";

const routes = {
`${root}`: () => <Users />,
};

当我尝试上面的代码时,出现以下错误:
Left side of comma operator is unused and has no side effects

最佳答案

The syntax for a computed property name在对象文字中是 [someExpression] , 不是 `${someExpression}` :

const root = "/";

const routes = {
[root]: () => <Users />,
};

[ Playground Link ]

关于string - typescript - 逗号运算符的左侧未使用且没有副作用 - 如何在 hookrouter 的路由中使用常量代替字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356722/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com