每当我尝试在React Native中编译时,我都会得到TypeError。下面是我试图创建实例的类:
class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
};
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}
我刚刚创建了:
const home = new GameTeam('name', 'null');
我也试过将const改为var,但没有任何效果。
如果它有助于解决问题,下面是主文件
import { GameTeam } from './objects/Team'
import React from 'react';
import { Image, StyleSheet, Text, View } from 'react-native';
import { useFonts } from 'expo-font'
import { VersusScore } from './components/VersusScore';
export default function App() {
const home = new GameTeam('Tigers', 'null');
const away = new GameTeam('Raiders', 'null');
return (
<View style={mainStyles.container}>
<View style={mainStyles.topBar}>
<Image style={mainStyles.navIcon_small} source={require('./assets/Icons/Inactive/Settings_Icon.png')} />
<Text style={mainStyles.Title}>BRC</Text>
<Image style={mainStyles.navIcon_small} source={require('./assets/Icons/Inactive/Announcements_Icon.png')} />
</View>
<VersusScore homeTName={home} awayTName={away} />
</View>
);
}
我所看到的唯一不同之处在于这个文档中有一个;在构造函数的末尾,可能不会给出语法错误,也可能阻止它工作
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
原来我需要改变class GameTeam
到第一个文件中的export default class GameTeam
,因为它是一个单独的文件,现在看起来像这样:
export default class GameTeam {
constructor(name, logoLocation)
{
this.name = name;
this.logoLocation = logoLocation;
}
getTeamName() {
return this.name;
}
getTeamLogoLocation() {
return this.logoLocation
}
}