使用自定义扩展名加载资产不起作用 [英] Loading asset with custom extension not working

查看:60
本文介绍了使用自定义扩展名加载资产不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在react-native中实现我的第一个应用程序,我需要从保存在项目文件夹中的静态文件中打开数据库.

I'm trying to implement my first application in react-native and I need to open database from a static file saved in my project folder.

我了解到我需要允许从资产加载自定义扩展文件,因此我在我的app.json文件中添加了以下片段:

I read that i need to allow loading custom extensions files from assets so i added following fragment into my app.json file:

"packagerOpts": {
    "assetExts": ["sqlite", "db"]
},

接下来,我要尝试在componentDidMount()方法中的App.js组件内以.sqlite或.db扩展名导入此静态文件:

Next I'm trying to import this static file with .sqlite or .db extension inside my App.js component in componentDidMount() method:

componentDidMount = async () => {
  await Expo.FileSystem.downloadAsync(
    Expo.Asset.fromModule(require("./assets/db/local.db")).uri,
    `${Expo.FileSystem.documentDirectory}SQLite/local.db`
  );

  SQLite.openDatabase("local.db");
};

,但博览馆建设者一直在说Unable to resolve "./assets/db/local.db" from "App.js".有什么建议吗?

but expo builder keep saying Unable to resolve "./assets/db/local.db" from "App.js". Any suggestion please?

推荐答案

下面的代码来自上面的2个答案

The following code is from 2 answers above

在项目根目录中创建 metro.config.js :

const defaultAssetExts = require("metro-config/src/defaults/defaults").assetExts;

module.exports = {
  resolver: {
    assetExts: [
      ...defaultAssetExts,
      // 3D Model formats
      "dae",
      "obj",
      "mtl",
      // sqlite format
      "db",
      "sqlite"
    ]
  }
};

(可选)安装Metro依赖项:npm i metro-config --save-dev

Optionally install metro-dependency: npm i metro-config --save-dev

这篇关于使用自定义扩展名加载资产不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆