gpt4 book ai didi

javascript - "TypeError: undefined is not an object (evaluating ' _expo.default.Font ')]"异步加载字体时遇到这个错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:39:56 26 4
gpt4 key购买 nike

这是我的错误:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expo.default.Font')] * app\views\Login.js:33:15 in componentWillMount$

最初我收到如下错误:

fontFamily "Roboto_medium" is not a system font and has not been loaded through Font.loadAsync. If you intended to use a system font, make sure you typed the name correctly and that it is supported by your device operating system.

所以我使用异步加载来加载字体,但开始出现上述错误。

import React, { Component } from "react";
import { Alert, AsyncStorage, StyleSheet, Text } from "react-native";
import {Container,Header,Content,Card,CardItem,Body,Form,Input,Button,Item
} from "native-base";
import { AppHeader } from "../sections/Header";
import Expo from "expo";
import {Font} from 'expo';
export class Login extends Component {
static navigationOptions = {
header: null
};

constructor(props) {
super(props);
this.state = {
username: "",
password: "",
isReady: false
};
}

async componentWillMount() {
await Expo.Font.loadAsync({
Roboto: require("native-base/Fonts/Roboto.ttf"),
Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf")
});
this.setState({ isReady: true });
}

render() {
if (!this.state.isReady) {
return <Expo.AppLoading />;
}
return (
<Container>
<AppHeader />
</Container>
);
}
}

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expo.default.Font')] * app\views\Login.js:33:15 in componentWillMount$ - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:271:30 in invoke - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:135:28 in invoke - node_modules\regenerator-runtime\runtime.js:170:17 in - node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo - node_modules\promise\setimmediate\core.js:200:23 in doResolve

这是我面临的错误

最佳答案

欢迎来到世博会!

这段代码有一些问题。

1、导出类使用“export default Class”而不是“export Class”

export default
  1. 在 react-native 中,你不需要构造函数。

  2. 在react中,你应该使用箭头函数而不是匿名函数,因为箭头函数是this mean class!

componentWillMount =  async() => {
  1. AppLoading 不是 UI 组件。使用 ActivityIndi​​cator。
if (!this.state.isReady) {
return <ActivityIndicator />
}

下面的代码运行良好。试试吧!

import React, { Component } from 'react'
import { ActivityIndicator } from 'react-native'
import { Container } from 'native-base'
import { AppHeader } from '../sections/Header'
import { Font } from 'expo'

export default class Login extends Component {
static navigationOptions = {
header: null
}

state = {
isReady: false
}

componentWillMount = async() => {
await Font.loadAsync({
Roboto: require('native-base/Fonts/Roboto.ttf'),
Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf')
})
this.setState({ isReady: true })
}

render () {
if (!this.state.isReady) {
return <ActivityIndicator />
}
return (
<View>
<AppHeader />
</View>
)
}
}

关于javascript - "TypeError: undefined is not an object (evaluating ' _expo.default.Font ')]"异步加载字体时遇到这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56449167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com