我是javaScript的新手,遇到了一个有趣的问题。这里是一些上下文;我运行的是Windows10,node v15,我已经下载了expo,并且正在使用visual studio。我用来运行代码的命令是npm start。
当我将代码添加到app.js的第4行时,问题就出现了。我正在尝试导入一个仅仅是一个按钮的文件。代码可以正常编译和加载,直到我添加了第4行。
我得到的错误是这样的;
"C:/Windows/System32/pleasework/App.js
找不到模块:无法解析“C:WindowsSystem32请工作”
感谢您的任何见解!
App.js代码
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import buttonWelcome from 'C:\Windows\System32\pleasework\node_modules\sweet.js';
export default class App extends Component() {
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Welcome</Text>
<buttonWelcome text='Login' color='red' />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 32,
textAlign: 'center',
margin: 10,
},
});按钮编码,文件名为sweet.js
import React from 'react';
import { StyleSheet, Text, View,TouchableOpacity } from 'react-native';
const buttonWelcome = props => {
const content = (
<View style={[style.button, {backgroundColor: props.color}]}>
<Text style={styles.text}>{props.text}</Text>
</View>
)
return <TouchableOpacity onPress={props.onPress}>{content}</TouchableOpacity>
}
const styles = StyleSheet.create({
button: {
padding: 16,
width: 200,
borderRadius: 24,
alignItems: 'center'
},
text: {
color: 'white',
fontSize: 20
}
})
export default buttonWelcome;发布于 2020-11-14 00:52:20
尝试使用带正斜杠的相对url:
import buttonWelcome from '../sweet.js';顺便说一句,你不应该把源文件放在node_modules中--那只适用于node_modules代码。
发布于 2020-11-14 00:52:40
导入语句错误。它应该是
import buttonWelcome from './sweet.js';正如你所看到的,它使用linux风格的文件路径,这一点很重要。这在linux和windows上都可以工作。
https://stackoverflow.com/questions/64824727
复制相似问题