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

问题描述 投票:1回答:1
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
string typescript react-hooks const
1个回答
1
投票

语法为 计算后的属性名称 在一个对象的字面意思是 [someExpression],不 `${someExpression}`:

const root = "/";

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

[游乐场链接]

© www.soinside.com 2019 - 2024. All rights reserved.