在React Native中使用decorator,可以't find变量:require

Using decorators in React Native, can't find variable: require

本文关键字:find 变量 require 可以 Native React decorator      更新时间:2023-12-05

我正试图在我的RN项目中使用装饰器:

以下是我的package.json文件:

{
  "name": "testApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "dependencies": {
    "autobind-decorator": "^1.3.3",
    "babel-core": "^6.7.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-class-properties": "^6.6.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-runtime": "^6.6.0",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-react-native": "^1.5.6",
    "babel-preset-stage-0": "^6.5.0",
    "babel-runtime": "^6.6.1",
    "react": "^0.14.8",
    "react-native": "^0.22.2",
    "webpack": "^1.12.14"
  }
}

这是我的。babelrc:

{
  "plugins": [
    "transform-runtime",
    "transform-decorators-legacy",
    "transform-class-properties"
  ],
  "presets": ["es2015", "react", "stage-0", "react-native"]
}

我在陈述ReferenceError: Can't find variable: require 时出错。我尝试了很多帖子,但都没有得到任何解决方案。我使用react-native init testApp 创建了新的RN项目

修复错误是因为预设的顺序:我删除了所有其他预设,并将预设更改为"本地反应",现在一切正常。