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

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

问题描述

我正在使用Nativescript教程在此处中创建轮播. >

我遇到的问题是我得到了以下错误(减去混淆)

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

当尝试在此行上加载xml文件时(以下完整代码段):

slides.push(builder.load(slidePath))

经检查,我发现文件系统看不到我正在加载的文件.我的代码与教程代码相同.我已经逐行(甚至做了一个差异),代码实际上是相同的.

在这里可以更好地查看它阻塞的文件路径,您可以将其与我在下面提供的图像进行比较: /data/data/{已混淆}​​/files/app/pages/welcome/slides/slide1.xml

我可以验证文件夹结构与教程app/pages/welcome/slides.slide1.xml中的文件夹结构相同,但是在页面加载时,我会收到该错误,并且它永远也不会加载xml.

这是完整的代码段:

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);
    });
  }

当我通过调试并使用文件系统模块测试路径是否存在时进行测试时...即使文件夹结构确实以教程中的方式存在,它也总是返回false.

console.log行显示以下内容:

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

如您所见,它与下面的我的文件夹路径匹配.

如何获取文件系统以查看该文件夹结构?当我将其用于验证存在的图像文件时,它工作得很好.

这是文件夹结构的图像:

解决方案

Webpack永远不会知道您将在运行时需要这些XML文件,因此您将不得不调整webpack.config.js以便将这些文件包括在捆绑软件中.

如下更新CopyWebpackPlugin配置,

        // 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)}/**`] },
        ),

添加{ 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天全站免登陆