我正在尝试使用Angular在NativeScript中访问android的R
对象,但没有取得任何成功。说明here表示要这样做:
android.R.color.holo_purple
但是当我尝试从android
模块或application
模块导入tns-core-modules/platform
变量时,我得到一个错误,即{{1}上的R
属性}对象未定义。如何获得对android
的访问权限?
答案 0 :(得分:2)
您不需要从android
模块导入application
变量。默认情况下在运行时自动可用。要消除编译时错误property doesn't exists
,只需将名为android
的变量声明为any
。
例如。
declare var android:any;
export class AppComponent{
let holoPurple=android.R.color.holo_purple;
}
答案 1 :(得分:0)
这里的android
是Android SDK的主要名称空间,而不是android
模块中的application
属性。
有关如何通过TypeScript访问本机SDK或如何使用this StackOverflow answer中的指令,请参见the related documentation article
答案 2 :(得分:0)
无法获得现有的答案以使用最新的本机脚本/ angular。我用以下方法解决了这个问题:
创建了一个文件App_Resources / Android / values / keys.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="key_name">xxxxxx</string>
</resources>
导入这些
import * as app from 'tns-core-modules/application';
import * as utils from 'tns-core-modules/utils/utils';
然后抓取字符串
const context = app.android.context;
const res = context.getResources().getString(utils.ad.resources.getStringId('key_name'));