Mobx返回ObservableObjectAdministration而不是我的对象

时间:2017-04-02 08:47:03

标签: javascript reactjs mobx mobx-react

我是mobx的新手。我想知道为什么当我调用计算的getServerUrls()函数而不是对象时,我得到ObservableObjectAdministration

enter image description here

以下是我的商店。

import { observable, computed } from 'mobx';
import { ServerNames } from 'master-server-urls';

class ServerNamesStores {

  @observable ServerNameUrls = {};
  @computed get getServerUrls() {
    console.log('@computed get getServerUrls()',  this.ServerNameUrls);
    return this.ServerNameUrls;
  }

  constructor() {
    const overrideServer = {
      "medicontentServer": "https://mediaserver.example.com"
    }
    const newServerNames = Object.assign({}, ServerNames, overrideServer);
    this.ServerNameUrls = newServerNames;
  }

}

const serverNamesStores = new ServerNamesStores();

export default serverNamesStores;
export { ServerNamesStores };

以下是我的App.js

import React, { Component } from 'react';
import { observer } from 'mobx-react';
import ServerNamesStores from './Stores/ServerNamesStores'
import logo from './logo.svg';
import './App.scss';

@observer
class App extends Component {

  constructor(props) {
    super(props)
    const { ServerNameUrls, getServerUrls } = ServerNamesStores
    console.log('ServerNameUrls', ServerNameUrls);
    console.log('1. getServerUrls', getServerUrls);
    console.log('2. getServerUrls', JSON.stringify(getServerUrls));
  }

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

访问该物业非常有用。下面的代码工作正常,将显示ServerNameUrls.webServer

的属性值
const { ServerNameUrls, getServerUrls } = ServerNamesStores
console.log('ServerNameUrls', ServerNameUrls.webServer)

1 个答案:

答案 0 :(得分:7)

我通过添加以下代码修复了它:)

import { toJS } from 'mobx';

然后使用

console.log('getServerUrls output', toJS(getServerUrls));

More info on MobX website