Reactjs read a properties file?

2020-03-30 04:23发布

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!

1条回答
迷人小祖宗
2楼-- · 2020-03-30 04:29

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>
        );
    }
}
查看更多
登录 后发表回答