0

使用 React Native 0.63 和 Typescript 4。

我有一个带有类别的应用程序。每个类别都有一个图像,该图像与代码一起存储为资产。

我试过这个:

myImage = require(`${myImageName}.jpg`)

但是 eslint 告诉我:

对 require() 的调用应使用字符串文字(eslintimport/no-dynamic-require

所以我这样做了:

在图像/index.ts 中:

import vibrators = require('./vibrators.jpg'),
import dildos = require('./dildos.jpg'),
import clitoris = require('./clitoris.jpg'),
import plugs = require('./plugs.jpg'),
import rings = require('./rings.jpg'),
import kits = require('./kits.jpg'),
import men = require('./men.jpg'),
import women = require('./men.jpg'),
import couple = require('./couple.jpg'),
import bondage = require('./bondage.jpg'),
import furniture = require('./funiture.jpg'),
import lingerie = require('./lingerie.jpg'),
import games = require('./games.jpg'),

const categories = {vibrators, dildos, plugs, rings, kits, men, women, couple, bondage, furniture, lingerie, games}

export { categories, ... }

(对不起,每次我发布有关此应用程序的内容时,我都厌倦了更改所有名称...^-^')

在我打印类别图像的组件中:

import React, { FC } from 'react'
import { TouchableOpacity, Image } from 'react-native'
import { useNavigation } from '@react-navigation/native'
import { categories } from '../assets/images'

interface Props {
  category: string
}

const CategoryImage: FC<Props> = ({ category }) => {
  const navigation = useNavigation()

  return (
    <TouchableOpacity style={styles.container} onPress={() => navigation.navigate(category)}>
      <Image source={categories[category]} style={styles.image} />
    </TouchableOpacity>
  )
}

export default CategoryImage

但又一次,在:

import vibrators = require('./vibrators.jpg'),

我有:

要求语句不是导入语句的一部分。( eslint@typescript-eslint/no-var-requires )

4

0 回答 0