最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reactjs read a properties file? - Stack Overflow

programmeradmin2浏览0评论

I have trouble looking for a solution on how to read a properties file in reactJS.

I've read that you can use the module "properties-reader" but I can't seem to make the require work. Is there a simple way ?

For example,

import React, { Component } from "react";
import './properties.file';
// var propertiesUser = properties.get('user');
class Main extends Component {

  render() {
    // Change title
    document.title = "Milo's Collection"

    return (
      <div>
        {propertiesUser}
      </div>

    );
  }
}

export default Main;
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>

I have trouble looking for a solution on how to read a properties file in reactJS.

I've read that you can use the module "properties-reader" but I can't seem to make the require work. Is there a simple way ?

For example,

import React, { Component } from "react";
import './properties.file';
// var propertiesUser = properties.get('user');
class Main extends Component {

  render() {
    // Change title
    document.title = "Milo's Collection"

    return (
      <div>
        {propertiesUser}
      </div>

    );
  }
}

export default Main;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

Can anyone help me? Thanks in advance guys!

Share Improve this question asked Apr 7, 2018 at 10:11 MichMich 3223 gold badges8 silver badges22 bronze badges 2
  • is this 'properties file' a JSON file? if so, how are you exporting it? and when you import it what's the specific issue – Jayce444 Commented Apr 7, 2018 at 10:49
  • yeah I can make it a json file, I just need a file where I can put a property that I use in other files, and when I change a property it'll change it in all the files I mean if i import a json file, how would I use the property in a var ? – Mich Commented Apr 7, 2018 at 10:58
Add a comment  | 

1 Answer 1

Reset to default 19

Here's a simple way to import/export an object (in a .js file) so you can re-use it across multiple files:

/* properties.js */

export const properties = {
    content: "This is a string"
};

Then in your react component:

import React, { Component } from 'react';
import { properties } from './properties.js';

class Main extends Component {

    render() {
        return (
            <div>
                {properties.content}
            </div>
        );
    }
}
发布评论

评论列表(0)

  1. 暂无评论