简体   繁体   English

react-native:导入后找不到变量样式表

[英]react-native: Can't find variable Stylesheet after importing it

I'm a bit perplexed on what should be a simple fix.我对什么应该是一个简单的修复有点困惑。 I'm running a react-native project on version 0.27.2, and all of my ios.js files successfully import Stylesheet except one.我在版本 0.27.2 上运行 react-native 项目,并且我的所有ios.js文件都成功导入样式表,除了一个。

The import looks like this:导入看起来像这样:

import React, {Component} from 'react';
import {
  AppRegistry,
  StyleSheet,
  ScrollView,
  Text,
  View,
} from 'react-native';

import {
  Cell,
  CustomCell,
  Section,
  TableView
} from 'react-native-tableview-simple';

const styles = Stylesheet.create({
  container: {
      backgroundColor: 'rgb(20,25,30)',
    }
  }
);

The Stylesheet.create({}) function is what ultimately causes the error Can't find variable Stylesheet to be thrown. Stylesheet.create({}) function 最终导致错误Can't find variable Stylesheet被抛出。 However, I've declared styles the same way in every other file with the same imports, and those rendered the styles with no error.但是,我已经在具有相同导入的所有其他文件中以相同的方式声明了 styles,并且那些呈现 styles 没有错误。

The only thing that's different in this file is that multiple classes have been declared.此文件中唯一不同的是声明了多个类。 I'm new to React, so I don't know if this could cause an issue.我是 React 新手,所以我不知道这是否会导致问题。 Does anyone have any idea what could be causing this?有谁知道是什么原因造成的?

You mixed the case, you need to write 你混合了情况,你需要写

const styles = StyleSheet.create({

like you named it when you imported it. 就像您在导入时为其命名一样。

I just run into same issue and found out that types are in different module.我刚遇到同样的问题,发现类型在不同的模块中。 If you have correct case(StyleSheet) and still doesn't work, it's very likely that you do not have type module installed.如果您有正确的大小写(样式表)但仍然无法正常工作,则很可能您没有安装类型模块。 Just execute this line in cmd -只需在 cmd 中执行这一行 -

npm install --save @types/react-native

It should do the trick.它应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM