提取本地JSON(在项目中)

我是NativeScript的新手,我想获取一个作为我项目的一部分的JSON。
我该怎么办?

以下失败:

app/data/util.js

import json from "../data/urls"; // Attempting to fetch urls.json

结果:

com.tns.NativeScriptException: Error calling module function 
Error: Cannot find module '../data/urls'
File: (file:///data/data/org.nativescript.preview/files/app/bundle.js:435:120)

StackTrace: 
    webpackMissingModule(file:///data/data/org.nativescript.preview/files/app/bundle.js:435:45)
    at (file:///data/data/org.nativescript.preview/files/app/bundle.js:435:131)
    at ./data/utils.js(file:///data/data/org.nativescript.preview/files/app/bundle.js:504:30)
    at __webpack_require__(file:///data/data/org.nativescript.preview/files/app/runtime.js:751:30)
    at fn(file:///data/data/org.nativescript.preview/files/app/runtime.js:121:20)
    at ../node_modules/babel-loader/lib/index.js!../node_modules/vue-loader/lib/index.js?!./components/Home.vue?vue&type=script&lang=js&(file:///data/data/org.nativescript.preview/files/app/bundle.js:9:69)
    at __webpack_require__(file:///data/data/org.nativescript.preview/files/app/runtime.js:751:30)
    at fn(file:///data/data/org.nativescript.preview/files/app/runtime.js:121:20)
    at ./components/Home.vue?vue&type=script&lang=js&(file:///data/data/org.nativescript.preview/files/app/bundle.js:397:187)
    at __webpack_require__(file:///data/data/org.nativescript.preview/files/app/runtime.js:751:30)
    at fn(file:///data/data/org.nativescript.preview/files/app/runtime.js:121:20)
    at ./components/Home.vue(file:///data/data/org.nativescript.preview/files/app/bundle.js:345:92)
    at __webpack_require__(file:///data/data/org.nativescript.preview/files/app/runtime.js:751:30)
    at fn(file:///data/data/org.nativescript.preview/files/app/runtime.js:121:20)
    at (file:///data/data/org.nativescript.preview/files/app/bundle.js:246:74)
    at ./app.js(file:///data/data/org.nativescript.preview/files/app/bundle.js:306:30)
    at __webpack_require__(file:///data/data/org.nativescript.preview/files/app/runtime.js:751:30)
    at checkDeferredmodules(file:///data/data/org.nativescript.preview/files/app/runtime.js:44:23)
    at webpackJsonpCallback(file:///data/data/org.nativescript.preview/files/app/runtime.js:31:19)
    at (file:///data/data/org.nativescript.preview/files/app/bundle.js:2:57)
    at require(:1:266)


Error: Cannot find module '../data/urls'
    at com.tns.Runtime.runmodule(Native Method)
    at com.tns.Runtime.runmodule(Runtime.java:662)
    at com.tns.Runtime.run(Runtime.java:654)
    at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
    at android.app.activityThread.handleBindApplication(activityThread.java:6158)
    at android.app.activityThread.access$1200(activityThread.java:235)
    at android.app.activityThread$H.handleMessage(activityThread.java:1768)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.activityThread.main(activityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

无论我尝试获取~/data/urls还是../data/urls,错误都是相同的(路径差异很小)

后来,我遇到了this question,但是我很确定有一种更简单的方法来获取当前项目的JSON,对吗?

zjc121 回答:提取本地JSON(在项目中)

您也应该确定文件扩展名, 所以应该像这样:

import json from "../data/urls.json";
本文链接:https://www.f2er.com/3161060.html

大家都在问