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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Typescript - 逗号运算符左侧未使用,没有副作用 - 如何在hookrouter的路由中使用常量代替字符串?相关的知识,希望对你有一定的参考价值。

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
答案

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

const root = "/";

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

[游乐场链接]

以上是关于Typescript - 逗号运算符左侧未使用,没有副作用 - 如何在hookrouter的路由中使用常量代替字符串?的主要内容,如果未能解决你的问题,请参考以下文章

Vue和Typescript:未找到图像导入

Perl中逗号运算符的执行顺序

未捕获的ReferenceError:使用btn按钮分配无效的左侧

ES6 扩展运算符 三个点(...)

typescript Javascript创建从数组中分离的逗号

使用浏览器工具未找到 BigQuery 表