React创建常量文件 [英] React create constants file

查看:446
本文介绍了React创建常量文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建常量文件,如:key - ReactJs中的值,

How to create constants file like: key - value in ReactJs,

ACTION_INVALID = "This action is invalid!"

并在其他组件中使用

errorMsg = myConstClass.ACTION_INVALID;


推荐答案

我不完全确定我有你的问题但是如果我这样做应该很简单:

I'm not entirely sure I got your question but if I did it should be quite simple:

根据我的理解,你只想创建一个包含常量的文件并在另一个文件中使用它。

From my understanding you just want to create a file with constants and use it in another file.

fileWithConstants.js:

fileWithConstants.js:

export const ACTION_INVALID = "This action is invalid!"
export const CONSTANT_NUMBER_1 = 'hello I am a constant';
export const CONSTANT_NUMBER_2 = 'hello I am also a constant';

fileThatUsesConstants.js:

fileThatUsesConstants.js:

import * as myConstClass from 'path/to/fileWithConstants';

const errorMsg = myConstClass.ACTION_INVALID;

如果你正在使用反应,你应该有webpack或packager(反应原生)所以你应该有babel可以将你的出口和进口翻译成旧的js。

If you are using react you should have either webpack or packager (for react-native) so you should have babel which can translate your use of export and import to older js.

这篇关于React创建常量文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆