首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typescript -逗号运算符的左侧未使用且没有副作用-如何在hookrouter的路由中使用常量代替字符串?

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

Stack Overflow用户
提问于 2020-06-13 15:18:30
回答 1查看 9.8K关注 0票数 3
代码语言:javascript
复制
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;

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

代码语言:javascript
复制
const root = "/";

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

当我尝试上面的代码时,我得到了以下错误:

代码语言:javascript
复制
Left side of comma operator is unused and has no side effects
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-13 16:22:28

对象文本中的The syntax for a [someExpression],而不是`${someExpression}`

代码语言:javascript
复制
const root = "/";

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

[Playground Link]

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62356722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档