Angular 6 - Cordova - How to convert current angul

2020-03-27 22:50发布

问题:

I have a fully developed angular 6 project and I want to build it to mobile app using cordova and ionic, but I have no idea how to convert the project to cordova-based project. The command shown in cordova instruction seems to create a whole new blank project, and migrating the project would really be a no go. I've searched around but found nearly no result mentioning this. Are there any one here can help?

回答1:

It's only follow the instructions in https://cordova.apache.org/docs/en/latest/guide/cli/. This guide create an "empty" app. only copy dist folder of your app to the www directory of the cordova project. You can use Visual Studio 2017 too and create a blank cordova project. Again, copy your dist folder to the www directory

But, before change your index.html adding the

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>

if you use cordova directy

or adding

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js">

if you use VisualStudio 2017

at end of your page (just before body close tag)

If you want to control the "back button" you can follow the instructions of Close angular modal and remain on same page on back button click

Update for Angular 8 you need make some change

Your need remove from index.html the "es-5" created, so your index becomes like:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">

    <title>App title</title>
    <!--IMPORTANT base href="./"--->
    <base href="./"> 
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <app-root></app-root>
    <script type="text/javascript" src="cordova.js"></script>

    <script type="text/javascript" src="runtime-es2015.js"></script>
    <script type="text/javascript" src="polyfills-es2015.js"></script>
    <script type="text/javascript" src="scripts.js" defer></script>
    <script type="text/javascript" src="main-es2015.js"></script>


</body>
</html>

NOTE: use

ng build --prod=true --outputHashing=none --namedChunks=false --vendorChunk=false

To build the application