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

javascript - Mobx返回ObservableObjectAdministration而不是我的对象-LMLPHP

下面是我的商店。

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;

访问该属性(property)效果很好。下面的代码工作正常,并将显示ServerNameUrls.webServer的属性值
const { ServerNameUrls, getServerUrls } = ServerNamesStores
console.log('ServerNameUrls', ServerNameUrls.webServer)

最佳答案

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

import { toJS } from 'mobx';

然后在用法上
console.log('getServerUrls output', toJS(getServerUrls));

More info on MobX website

关于javascript - Mobx返回ObservableObjectAdministration而不是我的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43166411/

10-13 03:09