首页 > 解决方案 > web.js 和 native.js 平台目标在 React Native Web 文件名中不起作用

问题描述

*.web.js并且*.native.js平台定位在 React Native Web 文件命名中不起作用。此路径是正确的import { Foo } from './Foo/Foo';,但它返回以下错误:

Module build failed: Error: ENOENT: no such file or directory

标签: reactjsreact-native

解决方案


目前这方面的文档很少,所以我会全部映射出来。这仅在路径通过src目录返回时才对我有用,即,而不是在从其他组件导入时../components/Foo/Foo使用更直接的路径。./Foo/Foo这是工作文件:

src/components/Foo/Foo.native.js

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

class FooNative extends Component {
  render() {
    return (
        <View></View>
    )
  }
}

export { FooNative as Foo };

src/components/Foo/Foo.web.js

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

class FooWeb extends Component {
  render() {
    return (
        <View></View>
    )
  }
}

export { FooWeb as Foo };

src/components/Bar.js

import React, { Component } from 'react';
import { Foo } from '../components/Foo/Foo';

export default class Bar extends Component {
  render() {
    return (
        <Foo></Foo>
    )
  }
}

推荐阅读