React:拆分的类型错误:无法读取未定义的属性(读取“拆分”)

Yas*_*ins 1 split typescript reactjs react-native

我试图通过 ap\xc4\xb1 获取数据,我确实做到了。但是,我在拆分方面遇到问题。我尝试了所有方法,但仍然出现此错误。我怎样才能做到这一点?

\n

编辑:我安装了这个$npm add react-split \n 并像这样导入它:import Split from 'react-split'。\n这是它在代码中的用法:

\n
import React, {Component} from "react";\nimport { Link } from "react-router-dom";\n\n class PokemonDetail extends Component {\n    constructor(props) {\n    super(props);\n    this.state = {\n    name: '',\n    imgUrl: '',\n    pokemonIndex: '',\n    \n  };\n}\ncomponentDidMount() {\n    const {name, url} = this.props;\n    const pokemonIndex = url.split('/')[url.split('/').length - 2];\n    const imageUrl = `https://github.com/PokeAPI/sprites/blob/master/sprites/pokemon/${pokemonIndex}.png?raw=true`;\n    \n    this.setState ({\n       name: name,\n       imageUrl: imageUrl,\n       pokemonIndex: pokemonIndex \n    });\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Yas*_*ins 5

我通过在分割之前插入问号解决了这个问题。我不知道原因,但它有效。

        const pokemonIndex = url?.split('/')[url?.split('/').length - 2];
Run Code Online (Sandbox Code Playgroud)

感谢所有回复我帖子的人。