the*_*e64 5 javascript ecmascript-6 reactjs react-native
我是 React Native 的新手。我一直在处理这个大项目,它包含太多可以在项目中的许多地方重复使用的字符串。所以我创建了一个strings.js
文件,就像在 android 中一样strings.xml
,将所有可重用的字符串存储在这样的一个文件中,
export const SOME_STRING = 'Some value';
export const ANOTHER_STRING = 'Another value';
...
Run Code Online (Sandbox Code Playgroud)
并在我需要时导入。
所以这些是我的问题...
1)这是一个好方法吗?
2)有什么替代方法吗?
您不需要导出每个值。我知道的一种更好的方法是导出
const SOME_STRING = 'Some value';
const ANOTHER_STRING = 'Another value';
module.exports = {
SOME_STRING:SOME_STRING,
ANOTHER_STRING:ANOTHER_STRING
}
Run Code Online (Sandbox Code Playgroud)
或者您可能希望将所有这些都包装在 1 个常量对象中
const APPLICATION_CONSTANTS = {
SOME_STRING : 'Some string',
ANOTHER_STRING : 'Another string'
}
export default APPLICATION_CONSTANTS;
Run Code Online (Sandbox Code Playgroud)
用法
import APPLICATION_CONSTANTS from './strings';
APPLICATION_CONSTANTS.SOME_STRING
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2493 次 |
最近记录: |