我得到的模块`。/ index.css`无法找到来自react-native

时间:2018-05-09 06:18:09

标签: reactjs react-native react-native-android react-native-ios

我收到了无法找到模块./index.css来自react-native,

我已在该位置正确导入文件。在谷歌搜索了很多,请帮助。

以下是错误的完整输出

Body:
{"originModulePath":"/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js","targetModuleName":"./index.css","message":"Unable to resolve module `./index.css` from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`: The module `./index.css` could not be found from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`. Indeed, none of these files exist:\n\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`","errors":[{"description":"Unable to resolve module `./index.css` from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`: The module `./index.css` could not be found from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`. Indeed, none of these files exist:\n\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`"}],"name":"Error","stack":"Error: Unable to resolve module `./index.css` from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`: The module `./index.css` could not be found from `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.js`. Indeed, none of these files exist:\n\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`\n  * `/Users/tejas-mac/Documents/React Studio/Exported Projects/index.css/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json)`\n    at ModuleResolver.resolveDependency (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:161:851)\n    at ResolutionRequest.resolveDependency (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:91:16)\n    at DependencyGraph.resolveDependency (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/node-haste/DependencyGraph.js:272:4579)\n    at dependencies.map.relativePath (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/DeltaBundler/traverseDependencies.js:376:19)\n    at Array.map (<anonymous>)\n    at resolveDependencies (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/DeltaBundler/traverseDependencies.js:374:16)\n    at /Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/DeltaBundler/traverseDependencies.js:212:33\n    at Generator.next (<anonymous>)\n    at step (/Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:313)\n    at /Users/tejas-mac/Documents/React Studio/Exported Projects/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:473"}
processBundleResult
    BundleDownloader.java:266
access$200
    BundleDownloader.java:35
onResponse
    BundleDownloader.java:153
execute
    RealCall.java:135
run
    NamedRunnable.java:32
runWorker
    ThreadPoolExecutor.java:1133
run
    ThreadPoolExecutor.java:607
run
    Thread.java:761

1 个答案:

答案 0 :(得分:2)

根据React Native Documentation:

您不使用特殊语言或语法来定义样式。您只需使用JavaScript设置应用程序的样式。所有核心组件都接受名为style的prop。样式名称和值通常与CSS在Web上的工作方式相匹配,除了名称是使用驼峰外壳编写的,例如backgroundColor而不是background-color

style道具可以是普通的旧JavaScript对象。

随着组件的复杂性增加,使用StyleSheet.create在一个地方定义多个样式通常更简洁。这是一个例子:

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

export default class LotsOfStyles extends Component {
  render() {
    return (
      <View>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigblue}>just bigblue</Text>
        <Text style={[styles.bigblue, styles.red]}>bigblue, then red</Text>
        <Text style={[styles.red, styles.bigblue]}>red, then bigblue</Text>
      </View>
    );
   }
 }

const styles = StyleSheet.create({
  bigblue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

当然,样式不需要与组件在同一个文件中。您可以将它们抽象为单独的文件,然后只需从那里导出它们并在需要时导入。