[英]React and TypeScript - how to import Less
I'm trying to import LESS classes inside my ReactJS component. 我试图在我的ReactJS组件中导入LESS类。
I'm using react-autosuggest
and according to documentation (for JS) i should do it like this: 我正在使用
react-autosuggest
,根据文档(对于JS),我应该这样做:
//file: myTheme.css
.container { ... }
.input { ... }
.suggestionsContainer { ... }
.suggestion { ... }
.suggestionFocused { ... }
and then, in my component import it and use like this: 然后,在我的组件中将其导入并像这样使用:
import theme from 'theme.css';
// ...
<Autosuggest theme={theme} ... />
The problem is, that TypeScript does not allow me to import css file like this. 问题是TypeScript不允许我这样导入css文件。 It says:
它说:
error TS2307: Cannot find module '../../../Stylesheets/AutosuggestStyles.css'
Another problem is that I would like to use Less
for styling my components. 另一个问题是我想使用
Less
来设计组件的样式。 I have no idea how should I do that. 我不知道该怎么办。
How can I do that (if not able to do with Less, CSS is enough for me) 我该怎么做(如果不能用Less,CSS就足够了)
How can I do that
我怎样才能做到这一点
Just declare it in a file globals.d.ts
只需在文件
globals.d.ts
声明它
declare module "theme.css";
https://basarat.gitbooks.io/typescript/content/docs/types/ambient/intro.html https://basarat.gitbooks.io/typescript/content/docs/types/ambient/intro.html
We use typed-css-modules-loader for webpack. 我们将type-css-modules-loader用于webpack。 It generates d.ts typings by watcher.
它由观察者生成d.ts类型。
npm install less-loader style-loader css-loader typed-css-modules-loader --save-dev npm install less-loader样式加载器css-loader typed-css-modules-loader --save-dev
Add to webpack.config, to rules section (if you're using Webpack 2) 添加到webpack.config的“规则”部分(如果使用的是Webpack 2)
{ test: /\\.less$/, include: jsDir, exclude: commonExcludePaths, use: [ 'style-loader', 'css-loader?modules&importLoaders=1&localIdentName=\\'[path][name]-[local]--[hash:base64:5]\\'', 'typed-css-modules-loader', ]}
Sometimes TS compiler doesn't see new typings, you need wait or re-run your watcher. 有时TS编译器看不到新类型,您需要等待或重新运行观察程序。 Hope this helps
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.