You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue+Cordova Android11项目用cordova-universal-links-plugin报错:xmlPreferences.hasOwnProperty不是函数

问题原因

这个错误是由于cordova-universal-links-plugin的维护进度滞后,未适配Cordova Android 11的API变更——新版本中xmlPreferences的类型从对象变为数组,导致调用对象专属的hasOwnProperty方法时触发报错。

解决建议

1. 替换为适配性更好的插件

放弃原插件,改用社区维护更活跃的替代方案:

  • 使用cordova-plugin-universal-links(注意名称差异),它对高版本Cordova Android兼容性更佳。执行以下命令切换:
    cordova plugin remove cordova-universal-links-plugin
    cordova plugin add cordova-plugin-universal-links
    

2. 手动修改插件源码临时修复

若必须使用原插件,可修改其钩子脚本适配数组类型的xmlPreferences:

  1. 找到插件目录下的钩子文件:plugins/cordova-universal-links-plugin/hooks中的相关js文件
  2. 定位到调用xmlPreferences.hasOwnProperty的代码段,替换为兼容数组的判断逻辑:
    原代码示例:
if (xmlPreferences.hasOwnProperty('yourPreference')) {
  // 原有逻辑
}

修改为:

// 兼容对象和数组两种类型的判断
const hasTargetPref = typeof xmlPreferences === 'object' && xmlPreferences !== null
  ? ('yourPreference' in xmlPreferences)
  : xmlPreferences.some(item => item.name === 'yourPreference');

if (hasTargetPref) {
  // 原有逻辑
}

3. 临时降级Cordova Android版本

若上述方案暂时无法落地,可降级到兼容原插件的Cordova Android版本:

cordova platform remove android
cordova platform add android@10.1.2

4. 手动配置Universal Links(无需插件)

绕开插件直接完成配置,步骤如下:

  • 修改AndroidManifest.xml:在主Activity标签内添加处理Universal Links的intent-filter:
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="https"
            android:host="your-domain.com" />
    </intent-filter>
    
  • 配置assetlinks.json:生成包含应用SHA256指纹和包名的配置文件,上传到域名的/.well-known/assetlinks.json路径。文件示例:
    [
        {
            "relation": ["delegate_permission/common.handle_all_urls"],
            "target": {
                "namespace": "android_app",
                "package_name": "com.your.app.package",
                "sha256_cert_fingerprints": ["你的应用SHA256指纹"]
            }
        }
    ]
    
  • Vue内处理跳转:在App.vue中监听Cordova事件,获取跳转链接并处理路由:
    document.addEventListener('deviceready', () => {
        window.addEventListener('resume', () => {
            const deepLinkUrl = window.location.href;
            // 根据链接跳转对应Vue路由
        });
    });
    

内容的提问来源于stack exchange,提问作者Hesam Mohseni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:57:30