使用 Angular 2 的 Cordova 应用程序 [英] Cordova application using Angular 2

查看:30
本文介绍了使用 Angular 2 的 Cordova 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cordova框架而不是离子.

Instead of ionic I would like to use cordova framework.

到目前为止,

第 1 步:

我创建了一个 angular 2 应用程序.

I have created an angular 2 application.

第 2 步:

我创建了一个cordova应用程序并在其中集成了angular 2应用程序.

I have created an cordova application and integrated angular 2 application in it.

运行成功.

第 3 步:

下一步是在加载时加载cordova.js文件

Next step is to load cordova.js file on load

第 4 步:

在我的项目中添加cordova插件(如camera、device ext).

Add cordova plugin(like camera , device ext) in my project.

已完成第 1 步和第 2 步.

Step 1 and 2 completed.

请帮助我完成第 3 步和第 4 步.

当我调用插件时,它会抛出如下错误,

When I call plugin its throws an error as follows,

推荐答案

要在 agular 项目中添加任何 Cordova 插件,

To add any Cordova plugin in agular project,

  1. 编辑 main.ts 如下:

  1. Edit main.ts to look like this:

if (environment.production) {enableProdMode();}让 onDeviceReady = () => {平台浏览器动态().bootstrapModule(AppModule).catch(err => console.error(err));

if (environment.production) { enableProdMode(); } let onDeviceReady = () => { platformBrowserDynamic() .bootstrapModule(AppModule) .catch(err => console.error(err));

};document.addEventListener("deviceready", onDeviceReady, false);

}; document.addEventListener("deviceready", onDeviceReady, false);

对于任何插件(例如相机):在上面,声明 let navigator: any;

For any plugin(for example Camera): on top, declare let navigator: any;

使用:navigator.camera.getPicture(成功,失败,{质量:80});.

use: navigator.camera.getPicture(success, failure, { quality: 80 }); .

在 angular 项目的 index.html 中添加以下脚本.

In index.html of the angular project add the following script.

<script type="application/javascript" src="cordova.js"></script>
注意:MIME 类型应该是application/javascript"而不是text/javascript".

<script type="application/javascript" src="cordova.js"></script>
Note: The MIME type should be "application/javascript" not "text/javascript".

使用命令构建项目

ng build --base-href .--prod --output-path ./Aditya/www/注意:这里 Aditya 是cordova 项目的名称

ng build --base-href . --prod --output-path ./Aditya/www/ Note: here Aditya is name of the cordova project

现在将 Cordova 项目 Aditya/www/文件夹中的 index.html 文件编辑为包括所有脚本的 MIME.

Now edit the index.html file inside Aditya/www/ folder of Cordova project to include MIME for all the script included.

type="application/javascript"

现在你可以走了.

这篇关于使用 Angular 2 的 Cordova 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆