【问题标题】:Javascript/Typescript: Use a variable like const { var as othername } = ...? [duplicate]Javascript/Typescript:使用 const { var as othername } = ... 之类的变量[复制]
【发布时间】:2021-11-02 07:39:17
【问题描述】:

我想这样做:

const { colors as ThemeColors } = useTheme();

但我不能......而且我不想这样做:

const { colors } = useTheme();
const ThemeColors = colors;

你知道怎么做吗?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    你可以这样做来创建一个别名(我不知道为什么它被称为别名,因为对我来说别名是通过引用复制的):

    const { colors: ThemeColors } = useTheme();
    

    但是,这不是通过引用复制的,而是作为值复制的。这意味着更改 ThemeColors 不会更改 colors

    【讨论】:

    • 哪里叫别名?
    • 我觉得哪里不对? xD 但我听到很多人称它为aliasingalias
    猜你喜欢
    • 2021-07-24
    • 2014-03-13
    • 2021-10-19
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多