【问题标题】:Contentful with react native boilerplate - failed to load bundle内容与反应原生样板 - 未能加载包
【发布时间】:2019-01-17 09:34:13
【问题描述】:

我在尝试将 Contentful JavaScript SDK 与新的 React Native 应用程序一起使用时遇到问题。重建步骤:

react-native init MyApp npm install contentful --save

然后在 index.js 中:

import { createClient } from 'contentful';

然后构建/运行:react-native run-ios

在导入行到位后,它会抱怨缺少 http 模块。如果我使用来自 https://github.com/Khaledgarbaya/ContentfulReactNative-boilerplate 的 Khaled 的示例样板,那么它可以正常工作。

知道什么可能导致冲突吗?

我已经尝试了错误消息中的建议,但它们并没有解决问题。

感谢任何帮助/建议

【问题讨论】:

    标签: javascript react-native contentful


    【解决方案1】:

    可以导入浏览器版本的内容SDK,不依赖http库:

    const { createClient } = require('contentful/dist/contentful.browser.min.js')
    

    此解决方案的功劳归于维护者之一here

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我还没有找到任何将内容导入 React Native 项目的解决方案。我正在使用 react-native@0.49.5 和 contentful@5.0.5。

      我的解决方案是使用 fetch 和 Contentful Delivery API:https://www.contentful.com/developers/docs/references/content-delivery-api/

      这是我的主要查询的示例:

      fetch(`https://cdn.contentful.com/spaces/${this.space_id}/entries?access_token=${this.access_token}&include=2&content_type=list_group`, {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        }
      }).then((res) => {
        if(res.ok && res.status === 200){
      
          callback(JSON.parse(res._bodyText));
      
        } else {
          callback(null)
        }
      
      })
      

      它需要更多的解析,但它对于基本的东西效果很好。希望这会有所帮助,直到 contentful 或 react-native 解决了这个错误。

      【讨论】:

        猜你喜欢
        • 2012-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        相关资源
        最近更新 更多