NativeScript - 模块加载

NativeScript - 模块加载 首页 / NativeScript入门教程 / NativeScript - 模块加载

NativeScript模块包含一组打包为单个库的相关函数。

Application

应用程序包含特定于平台的移动应用程序实现。简单的核心模块定义如下-

const applicationModule=require("tns-core-modules/application");

Console

控制台模块用于记录消息。它具有以下方法-

console.log("My FirstApp project"); 
console.info("Native apps!"); 
console.warn("Warning message!"); 
console.error("Exception occurred");

application-settings

应用程序设置模块包含管理应用程序设置的方法。要添加此模块,无涯教程需要添加以下代码-

const appSettings=require("tns-core-modules/application-settings");

应用程序设置中可用的几种方法如下-

  • setBoolean(key:string,value:boolean)  -  设置布尔对象

  • setNumber(key:string,value:number)  -  设置数字对象

  • setString(key:string,value:string)          -  设置字符串对象

  • getAllKeys()                                                    包含所有存储的keys

  • hasKey(key:string)                                        检查key是否存在

  • clear                                                                  清除存储的值

  • remove                                                             根据key删除所有条目。

使用应用程序设置的一个简单示例如下-

function onNavigatingTo(args) { 
   appSettings.setBoolean("isTurnedOff", false);
   appSettings.setString("name", "nativescript"); 
   appSettings.setNumber("locationX", 54.321); 
   const isTurnedOn = appSettings.getBoolean("isTurnedOn"); 
   const username = appSettings.getString("username"); 
   const locationX = appSettings.getNumber("locationX"); 
   // Will return "not present" if there is no value for "noKey" 
   const someKey = appSettings.getString("noKey", "not present"); 
}
exports.onNavigatingTo = onNavigatingTo; 
function onClear() {
   // Removing a single entry via its key name 
   appSettings.remove("isTurnedOff"); 
   // Clearing the whole settings 
   appSettings.clear(); 
}

http

该模块用于处理 http 请求和响应。要在您的应用程序中添加此模块,请添加以下代码-

const httpModule=require("tns-core-modules/http");

可以使用以下方法发送数据-

getString   -  用于发出请求,并以字符串形式从URL下载数据。它定义如下-

httpModule.getString("https://.../get").then(
   (r) => { 
      viewModel.set("getStringResult", r); 
   }, (e) => 
   { 
   }
);

getJSON   -  用于从JSON访问数据。它定义如下-

httpModule.getJSON("https://.../get").then((r) => { 
}, (e) => { 
});

getImage  -  从指定的URL下载内容并返回ImageSource对象。它定义如下-

httpModule.getImage("https://.../image/jpeg").then((r) => { 
}, (e) => { 
});

getFile   -  它有两个参数URL和文件路径。

  • URL        -  下载数据。

  • File path -  将URL数据保存到文件中。它在下面定义-

httpModule.getFile("https://").then((resultFile) => { 
}, (e) => { 
});

request    -  它具有选项参数。它用于请求选项并返回HttpResponse对象。它定义如下-

httpModule.request({ 
   url: "https://.../get", 
   method: "GET" 
}).then((response) => { 
}, (e) => { 
});

Image-source

image-source模块用于保存图像。无涯教程可以使用以下语句添加此模块-

const imageSourceModule=require("tns-core-modules/image-source");

如果要从资源加载图像,请使用以下代码-

const imgFromResources=imageSourceModule.fromResource("icon");

要从本地文件添加图像,请使用以下命令-

const folder = fileSystemModule.knownFolders.currentApp(); 
const path = fileSystemModule.path.join(folder.path, "images/sample.png"); 
const imageFromLocalFile = imageSourceModule.fromFile(path);

要将图像保存到文件路径,请使用以下命令-

const img = imageSourceModule.fromFile(imagePath); 
const folderDest = fileSystemModule.knownFolders.documents(); 
const pathDest = fileSystemModule.path.join(folderDest.path, "sample.png"); 
const saved = img.saveToFile(pathDest, "png"); if (saved) { 
   console.log(" sample image saved successfully!"); 
}

Timer

该模块用于在特定时间间隔执行代码。要添加此内容,需要使用 require -

const timerModule=require("tns-core-modules/timer");

它基于两种方法-

    setTimeout  - 用于延迟执行。以毫秒为单位。

    setInterval   - 用于以特定间隔应用循环。

Trace

该模块对于调试很有用。它提供了日志记录信息。该模块可以表示为-

const traceModule=require("tns-core-modules/trace");

如果要在应用程序中启用,请使用以下命令-

traceModule.enable();

ui/image-cache

图像缓存模块用于处理图像下载请求并缓存下载的图像。这个模块可以表示如下-

const Cache=require("tns-core-modules/ui/image-cache").Cache;

connectivity

该模块用于接收所连接网络的连接信息。它可以表示为-

const connectivityModule=require("tns-core-modules/connectivity");

Functionality Modules

函数模块包括许多系统/平台特定的模块。一些重要的模块如下-

platform     - 用于显示有关设备的信息。可以如下定义-

const platformModule=require("tns-core-modules/platform");

fps-meter   - 用于每秒捕获帧。可以如下定义-

const fpsMeter=require("tns-core-modules/fps-meter");

file-system  - 用于与设备文件系统一起使用。它定义如下-

const fileSystemModule=require("tns-core-modules/file-system");

ui/gestures  - 用于处理UI手势。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

硅谷产品实战36讲 -〔曲晓音〕

MongoDB高手课 -〔唐建法(TJ)〕

Service Mesh实战 -〔马若飞〕

说透数字化转型 -〔付晓岩〕

Go 语言项目开发实战 -〔孔令飞〕

说透5G -〔杨四昌〕

Redis源码剖析与实战 -〔蒋德钧〕

林外 · 专利写作第一课 -〔林外〕

Vue 3 企业级项目实战课 -〔杨文坚〕

好记忆不如烂笔头。留下您的足迹吧 :)