元素描述符的.kind属性必须为“方法”或“字段”

时间:2018-10-03 13:31:56

标签: mobx mobx-react

我正在关注mobx-react-router的文档,但是尝试运行我的应用程序时,在浏览器中出现以下错误:

Uncaught TypeError: An element descriptor's .kind property must be either "method" or "field", but a decorator created an element descriptor with .kind "undefined"
    at _toElementDescriptor (app.js:49988)
    at _toElementFinisherExtras (app.js:49990)
    at _decorateElement (app.js:49980)
    at app.js:49976
    at Array.forEach (<anonymous>)
    at _decorateClass (app.js:49976)
    at _decorate (app.js:49958)
    at Module../src/App/UserStore.js (app.js:50012)
    at __webpack_require__ (bootstrap:19)
    at Module../src/index.js (index.js:1)

这是我初始化的方式:

const appContainer = document.getElementById('app');
if(appContainer) {
  const browserHistory = createBrowserHistory()
  const routingStore = new RouterStore();

  const stores = {
    users: userStore,
    routing: routingStore
  }

  const history = syncHistoryWithStore(browserHistory, routingStore);

  ReactDOM.render(
    (
      <Provider {...stores}>
        <Router history={history}>
          < App />
        </Router>
      </Provider>
    ),
  appContainer);
}

这就是我的用法:

@inject('routing')
@inject('users')
@observer
class App extends Component { ...

我的UserStore

import { observable, action, computed } from "mobx"

class UserStore {
  @observable users = [];

  @action addUser = (user) => {
    this.users.push(user)
  }

  @computed get userCount () {
    return this.users.length
  }
}

const store = new UserStore();
export default store;

我曾尝试向Google提出此错误,但未返回任何有用的结果。有什么想法我做错了吗?

3 个答案:

答案 0 :(得分:4)

如果您正在使用Babel 7,请安装装饰器支持:

npm i -D\
  @babel/plugin-proposal-class-properties\
  @babel/plugin-proposal-decorators

然后在您的.babelrcwebpack.config.js文件中启用它:

{
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true}],
        ["@babel/plugin-proposal-class-properties", { "loose": true}]
    ]
}

请注意,传统模式很重要(将装饰器提案放在首位)。非旧版模式为WIP

参考:https://mobx.js.org/best/decorators.html

答案 1 :(得分:1)

虽然接受的答案有效,但对于来自同一错误消息但上下文不同的任何人,这很可能是因为装饰者的签名随着the proposal的进行而更改。

使用{ "legacy": true }使用一种方法签名,而{"decoratorsBeforeExport": true }使用另一种方法签名。这两个标志不兼容。

您可以查看给定示例的情况

function log() {
  console.log(arguments)
}
class Foo
  @log
  bar() {
    console.log('hello')
  }
}
new Foo().bar()

使用{"decoratorsBeforeExport": true }将产生

[Arguments] {
  '0': Object [Descriptor] {
    kind: 'method',
    key: 'bar',
    placement: 'prototype',
    descriptor: {
      value: [Function: bar],
      writable: true,
      configurable: true,
      enumerable: false
    }
  }
}

{"legacy": true }将给您的地方

[Arguments] {
  '0': Foo {},
  '1': 'bar',
  '2': {
    value: [Function: bar],
    writable: true,
    enumerable: false,
    configurable: true
  }
}

使用传统语义,编写装饰器非常简单。返回其第0个参数的装饰器是no-op。您还可以在返回之前进行变异。使用新的语义,您可以描述装饰器,如下所示:

function log(obj) {
  console.log('I am called once, when the decorator is set')
  let fn = obj.descriptor.value
  obj.descriptor.value = function() {
    console.log('before invoke')
    fn()
    console.log('after invoke')
  }
  return obj
}

答案 2 :(得分:0)

我像这样更改了可观察对象,并且它可以工作(尽管不确定为什么):

import { observable, action, computed } from "mobx"

class UserStore {
  @observable users;

  constructor() {
    this.users = []
  }

  @action addUser = (user) => {
    this.users.push(user)
  }
}

const store = new UserStore();
export default store;