Nativescript fs 模块看不到文件夹或文件 [英] Nativescript fs module not seeing folder or files

查看:22
本文介绍了Nativescript fs 模块看不到文件夹或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nativescript 教程创建轮播

解决方案

Webpack 永远不会知道您在运行时需要这些 XML 文件,您必须调整 webpack.config.js 以包含这些包中的文件.

更新CopyWebpackPlugin配置如下,

//将资产复制到 out 目录.根据需要添加您自己的 glob.新的 CopyWebpackPlugin([{ from: { glob: "assets/**" } },{ from: { glob: "fonts/**" } },{ from: { glob: "**/*.jpg" } },{ from: { glob: "**/*.png" } },{ from: { glob: "**/*.xml" } },],{ 忽略:[`${relative(appPath, appResourcesFullPath)}/**`] },),

添加 { from: { glob: "**/*.xml" } }, 将所有 XML 文件以及文件夹结构复制到包中.

I'm using the Nativescript tutorial for creating a carousel here.

The problem I'm running into is that I get the following error (minus my obfuscation)

Error: Failed to load component from module: undefined.xml or file: /data/data/{Obfuscated}/files/app/pages/welcome/slides/slide1.xml

when it tries to load xml files on this line (full snippet below):

slides.push(builder.load(slidePath))

Upon some inspection I found that it's the file system that doesn't see the files I'm loading. My code is the same as the tutorials code. I've gone through it line by line (even doing a diff) and the code is in fact the same.

Here's a better look at the file path it's choking on, you can compare that to the image I provided below: /data/data/{Obfuscated}/files/app/pages/welcome/slides/slide1.xml

I can verify that the folder structure is the same as in the tutorial app/pages/welcome/slides.slide1.xml but when the page loads, I get that error and it never loads the xml.

Here's the full snippet:

private loadSlides(slideFiles, slidesPath) {
    return new Promise(function (resolve, reject) {
      const slides = []
      const currentAppFolder = fs.knownFolders.currentApp();
      const path = fs.path.normalize(currentAppFolder.path + "/" + slidesPath);
      slideFiles.forEach((dataFile, i) => {
        const slidePath = path + "/" + dataFile;
        console.log(slidePath);
        // Here's where it crashes
        slides.push(builder.load(slidePath))
      });

      resolve(slides);
    });
  }

When I test it out by debugging and using the file-system module to test whether the path exists... it always comes back false, even though the folder structure definitely exists the way it does in the tutorial.

The console.log line displays this:

/data/data/{myobfuscation}/files/app/pages/welcome/slides

As you can see it matches my folder path below.

How do I get the file-system to see that folder structure? It works just fine when I use it for verifying the existence image files.

Here's an image of the folder structure:

解决方案

Webpack will never know you would require those XML files at runtime, you will have to adjust webpack.config.js to include those files in the bundle.

Update the CopyWebpackPlugin configuration as follows,

        // Copy assets to out dir. Add your own globs as needed.
        new CopyWebpackPlugin(
            [
                { from: { glob: "assets/**" } },
                { from: { glob: "fonts/**" } },
                { from: { glob: "**/*.jpg" } },
                { from: { glob: "**/*.png" } },
                { from: { glob: "**/*.xml" } },
            ],
            { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] },
        ),

Adding { from: { glob: "**/*.xml" } }, copies all XML files along with folder structure into the bundle.

这篇关于Nativescript fs 模块看不到文件夹或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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