从基础组件导入找不到变量

IT技术 reactjs react-native
2021-05-24 02:27:26

我创建了一个 BaseComponent,它将在所有其他组件中扩展:

import React, { Component } from 'react'
import { ScrollView, Text, Image, View, TouchableOpacity, TextInput } from 'react-native'

export default class BaseComponent extends Component {

}

在我的其他组件中,我使用它,例如:

import BaseComponent from '../Components/BaseComponent'

export default class LoginScreen extends BaseComponent {

}

我现在已经进口ViewScrollView等在BaseComponent但是当我试图在子组件中使用它,它显示的错误can'find variable View

如何在 ChildComponent 中使用导入的 BaseComponnet 类?

1个回答

在 Nodejs 中,每个文件都被视为一个module,具有自己的变量范围。React or ScrollView例如,当您将变量导入文件时,您将此变量添加到module范围,而不是全局范围。

如果webpack您可以使用ProvidePlugin一些导入作为全局

new webpack.ProvidePlugin({
  React: 'react' // ReactJS module name in node_modules folder
})

之后,您可以跳过在所有module中导入这些变量变量,因为webpack它会在需要的地方自行处理。