从JavaScript插件访问config.xml首选项

jvi*_*tti 6 cordova

在我的项目中,config.xml我添加了一个自定义首选项:

<preference name="FooBar" value="Baz" />
Run Code Online (Sandbox Code Playgroud)

然后,在我的自定义插件的JavaScript里面,<plugin>/www/plugin.js我想访问这样的功能的值.

Cordova是否将这些值暴露给JavaScript方面?我在文档中找不到任何关于它的信息.

尝试:

var argscheck = require('cordova/argscheck');
argscheck.getValue('FooBar'); // Returns just FooBar
Run Code Online (Sandbox Code Playgroud)

cod*_*ion 4

您可以在 iOS、WP7、WP8、Windows8 和 Ubuntu 上使用以下代码

function readConfig() {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function () {
        var parser = new DOMParser();
        var doc = parser.parseFromString(xhr.responseText, "application/xml");
        alert("Description : " + 
              doc.getElementsByTagName("description").item(0).textContent);
    });
    xhr.open("get", "../config.xml", true);
    xhr.send();
}
Run Code Online (Sandbox Code Playgroud)

对于 Android,您必须将文件路径从 更改"../config.xml""../../android_res/xml/config.xml"

摘自科尔多瓦邮件讨论的答案: https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html

还有用于读取配置的非官方插件: https://github.com/apache/cordova-labs/tree/cdvtes ​​t/cordova-plugin-appsettings