返回博客首页
← 所有文章

NativeScript 7 导入参考指南

2020年8月31日 — 作者:技术指导委员会 (TSC)

在 NativeScript 7 中,我们简化了开发人员体验,以包含所有核心提供的正确导出的 es2017 符号,并从这个单一的导入中获取。

import { } from `@nativescript/core`;

我们还希望解决核心代码中大量通用命名的符号以及代码库中四处飞舞的问题,以尽可能避免导入别名。这也有助于隔离冲突的命名空间,并且未来会在此方面进行更多改进。

以下是一些有助于说明部分汇总的示例

之前

import * as app from 'tns-core-modules/application';
import { setString } from 'tns-core-modules/application-settings';
import { messageType, write } from 'tns-core-modules/trace';
import { request } from 'tns-core-modules/https';
import { startMonitoring, connectionType } from 'tns-core-modules/connectivity';
import { ObservableArray } from 'tns-core-modules/data/observable-array';
import { GridLayout } from 'tns-core-modules/ui/layouts/grid-layout';
import { KeyedTemplate, View } from 'tns-core-modules/ui/core/view';
import { layout, openUrl, isNullOrUndefined } from 'tns-core-modules/utils/utils';

// sample code
const rootView = app.getRootView();

setString('MyKey', 'Hello');

write('Some message', 'a category');

request({
    url: "https://httpbin.org/get",
    method: "GET"
}).then((response) => {
    // Argument (response) is HttpResponse
}, (e) => {
});

startMonitoring((cType: number) => {
		switch (cType) {
			case connectivity.connectionType.wifi:

        break;
    }
});

layout.getDisplayDensity();

openUrl('https://news.com/article');

if (isNullOrUndefined(aVariable)) {
  console.log('You may wanna define that.');
}

alert('Hi');

之后

import { 
  Application, 
  ApplicationSettings,
  Trace,
  Http,
  Connectivity,
  ObservableArray, 
  GridLayout, 
  KeyedTemplate, 
  View,
  Dialogs,
  Utils
} from '@nativescript/core';

// sample code
const rootView = Application.getRootView();

ApplicationSettings.setString('MyKey', 'Hello');

Trace.write('Some message', 'a category');

Http.request({
    url: "https://httpbin.org/get",
    method: "GET"
}).then((response) => {
    // Argument (response) is HttpResponse
}, (e) => {
});

Connectivity.startMonitoring((cType: number) => {
		switch (cType) {
			case Connectivity.connectionType.wifi:

        break;
    }
});

Utils.layout.getDisplayDensity();

Utils.openUrl('https://news.com/article');

if (Utils.isNullOrUndefined(aVariable)) {
  console.log('You may wanna define that.');
}

// you can still use global alert just fine but to better identify {N} dialog usage you can now use the `Dialogs` rollup which contains all the dialog methods and interfaces
Dialogs.alert('Hi');