2017-04-11 136 views
7

我已创建了创建反应的应用程序内一个ReactJs的应用程序,然后由生产建立与npm run build。在我使用Cordova创建的www文件夹中,我只需从create-react-app构建文件夹中复制所有文件即可。科尔多瓦与创建反应的应用程序内

我想知道我怎么挂接到科尔多瓦的事件,例如像:

function startApp() { 
    // your app logic 
} 
if (window.cordova) { 
    document.addEventListener('deviceready', startApp, false); 
} else { 
    startApp(); 
} 

比如我想调用里面startApp()缩小的JS文件。还是有任何其他工作流程可用于使科尔多瓦事件与反应应用程序一起工作。

一个小例子将是有益的。

是否有可能使用的生成文件在所有的,只是使用的直接反应内部应用程序科尔多瓦?由于Webpack设置将ES6代码转换为ES5以及全部代码,因此我不确定这是如何工作的。

我是新与科尔多瓦以及与此整合方面的努力。

+0

我已想出如何使这两个工作,并张贴在这里为别人寻找相同的答案。这是非常微不足道的,可能还有其他方法,但这对我来说是非常好的。 –

回答

10

我发现使这两个工作,并张贴在这里为别人寻找相同。也许有其他方法可以做到这一点,但这对我来说很有用。

所以基本上,我们将使用创建应用程序科尔多瓦(说): 科尔多瓦创建testapp com.test.testapp testapp 这会给我一个文件夹结构像这样:

testapp 
     --hooks 
     --platforms 
     --plugins 
     --www 
     --config.xml 

现在testapp文件夹中我们运行:创建反应的应用程序内teastappReact 这将增加我的反应testapp文件夹内的应用程序。 你反应的应用程序将有一个主index.js在/ src目录。

我的index.js一定要包装你的主逻辑函数中,然后调用函数与科尔多瓦对象一起,像这样:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
import './index.css'; 


const startApp =() => { 
ReactDOM.render(
    <App />, 
    document.getElementById('root') 
); 
} 

if(!window.cordova) { 
    startApp() 
} else { 
    document.addEventListener('deviceready', startApp, false) 
} 

这应该做,现在你的应用程序将沿着有科尔多瓦实例在你的应用中使用像navigator.camera这样的Device对象。

而且在你的反应可以在公共文件夹中找到的,你将在Codova WWW文件夹中找到的index.html复制HTML应用程序的index.html。现在我们可以从www文件夹中删除所有文件。我们稍后将手动或通过脚本将反应应用程序生成文件夹中的所有文件复制到Cordova www文件夹。

所以我的index.html看起来像下面,注意cordova.js文件多数民众赞成列入为脚本。

<!DOCTYPE html> 
<!-- 
    Licensed to the Apache Software Foundation (ASF) under one 
    or more contributor license agreements. See the NOTICE file 
    distributed with this work for additional information 
    regarding copyright ownership. The ASF licenses this file 
    to you under the Apache License, Version 2.0 (the 
    "License"); you may not use this file except in compliance 
    with the License. You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, 
    software distributed under the License is distributed on an 
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
    KIND, either express or implied. See the License for the 
    specific language governing permissions and limitations 
    under the License. 
--> 
<html> 

<head> 
    <!-- 
     Customize this policy to fit your own app's needs. For more guidance, see: 
      https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy 
     Some notes: 
      * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication 
      * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly 
      * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: 
       * Enable inline JS: add 'unsafe-inline' to default-src 
     --> 
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src * data: content:;"> 
    <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"> 
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> 

    <!-- Latest compiled and minified CSS --> 
    <title>React App</title> 
</head> 

<body> 
    <div id="root"></div> 
    <script type="text/javascript" src="cordova.js"></script> 
</body> 

</html> 

最后,在你的应用程序的反应的package.json添加以下行: .... ‘主页’:“../www” .... 这将确保您的最终版本文件指向正确的路径。 我们也可以在您的package.json构建脚本添加以下代码行。

"scripts": { 
    "start": "react-scripts start", 
    ***"build": "react-scripts build && robocopy .\\build ..\\www /MIR",*** 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject", 
    "deploy": "npm run build&&gh-pages -d build" 
    } 

它可以是基于OS(Windows/Linux等)的robocopy或cp-r。

我们应该有科尔多瓦的应用程序准备与 科尔多瓦构建Android/IOS来构建。

5

我解决了这个问题。下面是我在一步一步的格式没有任何人寻求的解决方案:

  1. 复制/创建-A-新React项目(使用create-react-app创建)只是Cordova app目录内。
  2. 清除www文件夹的所有内容Cordova应用程序。
  3. cd反应项目文件夹(你刚才复制/创建)&开放package.json
  4. dependencies之前添加"homepage": "./", &脚本内改变build"build": "react-scripts build && robocopy .\\build ..\\www /MIR",
  5. npm run build在同一(React的)目录&回到父母(Cordova)文件夹,然后buildemulate在期望的平台项目。
  6. 此外,如果你在你的项目中使用的变化是<Router><HashRouter>否则日子会把你看到一个空白显示为那什么事都渲染到屏幕上。