在React Native中将yaml文件导入而不是实际内容中,其结果为1

问题描述

我正在尝试在React Native中导入yaml文件。我在Metro defaults.js文件中看到yaml已经被列为资产扩展。

尽管,导入的值始终是数字1,而不是yaml文件的实际内容

import enYaml from '../i18n/locale/en.yaml';

Debugger screenshot

解决方法

那是因为您将其作为资源加载。所以它是资源ID。您需要的是 What is the equivalent of a webpack loader when using the metro bundler in React Native?

的答案

要在使用 Metro 使用的 babel.config.js 的 Expo 中执行此操作,您需要添加 babel-plugin-content-transformer 作为开发依赖项并按如下方式配置

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],plugins: [
      [
        'content-transformer',{
          transformers: [
            {
              file: /\.ya?ml$/,format: 'yaml',},],...