如何实现 reactjs 全局配置 [英] how to implement reactjs global configuration

查看:75
本文介绍了如何实现 reactjs 全局配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试关注 这个链接关于设置全局配置.我无法让它发挥作用.当我运行我的 reactjs 组件时,我收到一个错误,指出 Config 未定义.

我的 webpack.config 文件如下所示:

module.exports = {条目:'./src/app.js',外部:{'配置':JSON.stringify({选项服务:'http://localhost:8080/options'})},输出: {路径:__目录名,文件名:'bundle.js'},模块: {装载机: [{测试:/\.jsx?$/,排除:/node_modules/,loader: 'babel-loader',查询:{预设:['es2015','react']}}]}};

我试过这样使用它:

从'react'导入React;从配置"导入配置;//这些也失败//const Config = require('Config');//var Config = require('Config');//让 Config = require('Config');导出默认类 MyComponent 扩展 React.Component {使成为() {返回 (<div>{Config.optionsService}

);}}

我也试过注释掉的行,但没有可用.

我做错了什么?

谢谢马特

解决方案

$ npm install react-global-configuration

安装后:

从'react-global-configuration'导入配置;config.set({ foo: 'bar' });

获得你的价值

从'react-global-configuration'导入配置;config.get('foo');

I am trying to follow this link about setting up global configuration. And I am unable to make it work. When I run my reactjs component I get an error that Config is undefined.

my webpack.config file looks like this:

module.exports = {
  entry: './src/app.js',
  externals: {
    'Config': JSON.stringify({
      optionsService: 'http://localhost:8080/options'
    })
  },
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: { presets: [ 'es2015', 'react' ] }
      }
    ]
  }
};

and I've tried using it like this:

import React from 'react';
import Config from 'Config';

// these also fail
// const Config = require('Config');
// var Config = require('Config');
// let Config = require('Config');


export default class MyComponent extends React.Component {
    render() {
        return (
            <div>
            {Config.optionsService}
            </div>
        );
    }
}

I've also tried the commented out lines, to no available.

What am I doing wrong?

Thank you Matt

解决方案

$ npm install react-global-configuration

After install:

import config from 'react-global-configuration';

config.set({ foo: 'bar' });

getting your value

import config from 'react-global-configuration';

config.get('foo');

这篇关于如何实现 reactjs 全局配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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