我正在尝试将所有CSS样式值存储到一个单独的ts
文件:styles/base.ts
中,并将这些值导出到styles/index.ts
文件中。但是,当我尝试使用index.ts
文件中的App.tsx
导入值时,它说
无法从“ App.tsx”解析模块“样式/索引”:“样式/索引”在项目内找不到。。
我不确定在应用程序内部管理样式的正确方法。谁能帮我吗?
这是我的base.ts
文件的外观:
export const colors = {
grey: "#E3E1D6",
black: "#333333",
};
index.ts:
import * as BaseStyle from "./base";
export {BaseStyle};
App.tsx:
import React from "react";
import {StyleSheet, View, Text} from "react-native";
import {BaseStyle} from "styles/index";
const App = () => {
return (
<>
<View style={styles.body}>
<Text>Test</Text>
</View>
</>
);
};
const styles = StyleSheet.create({
body: {
backgroundColor: BaseStyle.colors.grey,
},
});
export default App;
这是我的package.json看起来像:
{
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"react": "16.9.0",
"react-native": "0.61.4"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"@react-native-community/eslint-config": "^0.0.5",
"@types/jest": "^24.0.18",
"@types/react-native": "^0.60.22",
"@types/react-test-renderer": "16.9.0",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0",
"typescript": "^3.6.3"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
我认为这只是您输入中的错字。您应该写:
import {BaseStyle} from "./styles/index";
您可以使用此图案创建颜色类别
class Colors {
static transparents = "#ffffff";
static black = "#000";
}
export default Colors;
您可以使用此行导入颜色
import Colors from './Colors';
请按照以下步骤操作,这将为您提供帮助它可以帮助我。