javascript 在www.example中显示svg图标 www.example.com ,在类型'JSX'上不存在,IntrinsicElements'

gmxoilav  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(109)

我正在尝试Map我的数据以创建Map标记:

import ShellIcon from '../../../assets/images/xxx.svg';
import BPIcon from '../../../assets/images/xxx.svg';
import TexacoIcon from '../../../assets/images/xx.svg';

    return (
          <View style={styles.container}>
            ...
    
            <MapView
              ..>
              {staticData.map((marker, i) => {
                const MarkerIcon = marker.type;
    
                return (
                  <Marker key={index} coordinate={item.coordinates} ref={markerRef}>
                    <MapPin price={item.price} icon={MarkerIcon} />
                  </Marker>
                );
              })}
            </MapView>
          </View>
        );

贴图大头针:

const MapPin = ({ price, icon }: Props) => (
  <View style={styles.bubbleContainer}>
    <View style={styles.bubbleContent}>
      <icon />
      <Text style={styles.bubblePrice}>{price}</Text>
    </View>
    <View style={styles.bubbleArrow} />
  </View>
);

我的数据是:

const staticData = [
    {
      name: 'test station',
      type: 'ShellIcon',
      price: '15.00p',
      coordinates: {
        latitude: 52.56504897473243,
        longitude: -1.9835459043698045,
      },
    },
    {
      name: 'test station',
      type: 'BpIcon',
      price: '10.00p',
      coordinates: {
        latitude: 52.564655445785036,
        longitude: -1.9895132194946084,
      },
    },
    {
      name: 'test station',
      type: 'TexacoIcon',
      price: '12.00p',
      coordinates: {
        latitude: 52.56675992926686,
        longitude: -1.9925531724706291,
      },
    },
  ];
disho6za

disho6za1#

我相信你需要的是Map:

import ShellIcon from '../../../assets/images/xxx.svg';
import BPIcon from '../../../assets/images/xxx.svg';
import TexacoIcon from '../../../assets/images/xx.svg';

const ALL_ICONS = { ShellIcon, BPIcon, TexacoIcon };

return (
  <View style={styles.container}>
    <MapView>
      {staticData.map((marker, i) => {
        const MarkerIcon = ALL_ICONS[marker.type];
        return (
          <Marker key={index} coordinate={item.coordinates} ref={markerRef}>
            <MapPin price={item.price} icon={MarkerIcon} />
          </Marker>
        );
      })}
    </MapView>
  </View>
);

你不能只使用string的值来从import中获取变量(请注意,marker.typestring的类型,而你在icon中期望的是像SVG图标组件一样可构造的东西)

368yc8dk

368yc8dk2#

我认为这是因为你在map中初始化了marker,但你试图使用item来达到这个值。

import ShellIcon from '../../../assets/images/xxx.svg';
import BPIcon from '../../../assets/images/xxx.svg';
import TexacoIcon from '../../../assets/images/xx.svg';

    return (
          <View style={styles.container}>
            ...
    
            <MapView
              ..>
              {staticData.map((marker, i) => {
                const MarkerIcon = marker.type;
    
                return (
                  <Marker key={index} coordinate={marker.coordinates} ref={markerRef}>
                    <MapPin price={marker.price} icon={MarkerIcon} />
                  </Marker>
                );
              })}
            </MapView>
          </View>
        );

另外,将图标移动到staticData,如下所示:

const staticData = [
    {
      name: 'test station',
      type: <ShellIcon/>,
      price: '15.00p',
      coordinates: {
        latitude: 52.56504897473243,
        longitude: -1.9835459043698045,
      },
    },
    {
      name: 'test station',
      type: <BpIcon/>,
      price: '10.00p',
      coordinates: {
        latitude: 52.564655445785036,
        longitude: -1.9895132194946084,
      },
    },
    {
      name: 'test station',
      type: <TexacoIcon/>,
      price: '12.00p',
      coordinates: {
        latitude: 52.56675992926686,
        longitude: -1.9925531724706291,
      },
    },
  ];

也可以修改MapPin

const MapPin = ({ price, icon }: Props) => (
  <View style={styles.bubbleContainer}>
    <View style={styles.bubbleContent}>
      {icon}
      <Text style={styles.bubblePrice}>{price}</Text>
    </View>
    <View style={styles.bubbleArrow} />
  </View>
);

如果你不想这样做,你需要编写switch case,以便通过使用type from staticData传递MarkerIcon。

相关问题