简体   繁体   English

在React中导入TypeScript变量

[英]Import TypeScript variable in React

I am trying to style (styled-components) a fabric js component (Dropdown) as a react component. 我正在尝试将面料js组件(Dropdown)样式化(styled-components)作为react组件。

The css class names are declared in a file office-ui-fabric-react/lib/components/Dropdown/Dropdown.scss.d.ts. css类名称在文件office-ui-fabric-react / lib / components / Dropdown / Dropdown.scss.d.ts中声明。 Example: 例:

export declare const root = "root_15a7b352";

I want to import this class name so I can use it in styled. 我想导入该类名,以便可以在样式中使用它。

AFAIK this is TypeScript global variable and I tried looking for information on how to get to it but with no success. AFAIK这是TypeScript全局变量,我尝试寻找有关如何获取它的信息,但没有成功。

The statement declare const root is not the same as const root . 该语句declare const rootconst root It means: 它的意思是:

There is a variable called root somewhere else, and I'm just here to describe it. 在其他地方有一个叫做root的变量,我只是在这里描述它。

In other words, it tells us something on the type level, but it doesn't contain any executable code. 换句话说,它告诉我们有关类型级别的信息,但其中不包含任何可执行代码。 It's not possible to consume it in runtime. 在运行时无法使用它。 You can verify it by inspecting the generated code in TypeScript playground . 您可以通过检查TypeScript Playground中生成的代码来验证它。

Most likely it's meant to be imported from someplace else, or is, in fact, a global variable Since the declaration file describes Dropdown.scss , it's likely that root is a CSS class living in that file. 实际上,它很可能是从其他地方导入的,或者实际上是全局变量。由于声明文件描述了Dropdown.scss ,因此root很可能是该文件中的CSS类。 Try: 尝试:

import { root } from 'office-ui-fabric-react/lib/components/Dropdown/Dropdown.scss'

Karol Majeswski's Answer gets it right on importing the variable from typescript. Karol Majeswski的“答案”正确地从打字稿中导入了变量。

However, it seems your intention is to style parts of the dropdown using styled-components , instead of importing this variable from scss file, the Dropdown component provides readable classnames like ms-Dropdown-title that can be used for styling. 但是,您的意图似乎是使用styled-components下拉菜单的某些styled-components ,而不是从scss文件导入此变量,Dropdown组件提供了可读取的类名,例如ms-Dropdown-title ,可用于样式化。

For eg. 例如。 to style the Dropdown, you would use: 为下拉菜单设置样式,您可以使用:

const StyledDropdown = styled(Dropdown)`
  color: red;

  & .ms-Dropdown-title {
    background-color: red;
  }
`;

See Codepen example 请参阅Codepen示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM