导入默认导出未定义

时间:2019-07-05 11:56:53

标签: reactjs typescript webpack babel

我将TypeScript与Babel 7,Webpack 4和React结合使用。

export default () => <span>Test</span>

然后

import Test from './test'

此时测试未定义,或者plugin-transform-modules-commonjs为空对象。

.babelrc

  "plugins": [
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-arrow-functions",
    "@babel/plugin-transform-modules-commonjs",
    "react-hot-loader/babel"
  ],
  "presets": [
    "@babel/preset-react",
    "@babel/preset-typescript",
    ["@babel/preset-env", {
      "targets": {
        "browsers": "> 0.5%, last 2 versions, Firefox ESR, ie 11, not dead"
      }
    }]
  ]
}

tsconfig.json

{
    "compilerOptions": {
    "module": "common",
    "target": "es5",
    "noEmit": true,
    "strict": true,
    "skipLibCheck": true,
    "baseUrl": "./",
    "paths": {
      "*": ["declarations"]
    }
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ]
}

1 个答案:

答案 0 :(得分:1)

如果我没有记错的话,原来是public class MainActivity extends AppCompatActivity { Button A,C,D,B; ArrayList<Button> options = new ArrayList<Button>(); EditText P,Q,L,S; ArrayList<String> answers = new ArrayList<String>(); ArrayList<EditText> typeText = new ArrayList<EditText>(); public void changeButtonText(View view) { /* answers.add(P.getText().toString()); /* works fine when answers.add(Q.getText().toString()); this code is answers.add(L.getText().toString()); run*/ answers.add(S.getText().toString());*/ int j=0; for(j=0;j<3;j++) /*creates problem when this loop is run */ { answers.add(j,typeText.get(j).getText().toString()); } j=0; for(Button i:options) { i.setText((answers.get(j))); j++; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); P=(EditText) findViewById(R.id.editText1); Other EditText fields are declared the same way A=(Button) findViewById(R.id.button1); Other Buttons declare the same wat options.add(A); options.add(B); //Add buttons to Button ArrayList options.add(C); options.add(D); typeText.add(P); typeText.add(Q); //Add EditTexts to the ArrayList typeText.add(L); typeText.add(S); } } 导致了错误。