如何实现reactjs全局配置

问题描述 投票:0回答:1

我正在尝试关注this link关于设置全局配置。我无法使其发挥作用。当我运行reactjs组件时,我得到一个错误,即Config未定义。

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

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' ] }
      }
    ]
  }
};

我试过像这样使用它:

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>
        );
    }
}

我也尝试过注释掉的行,没有可用的。

我究竟做错了什么?

谢谢马特

webpack
1个回答
2
投票
$ npm install react-global-configuration

安装后:

import config from 'react-global-configuration';

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

获得你的价值

import config from 'react-global-configuration';

config.get('foo');
© www.soinside.com 2019 - 2024. All rights reserved.