2016-12-30 177 views
1

我正努力在Cordova中實現一個簡單的Vue.js應用程序。一切正常,但我不知道如何攔截cordova事件(deviceready,pause,...)到我的vue應用程序中。我使用了vue-cli的Webpack模板。Vue js in cordova

這是我的文件js/index.js

const app = { 
    initialize: function() { 
    console.log('initialize') 
    this.bindEvents() 
    }, 
    bindEvents: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false) 
    }, 
    onDeviceReady: function() { 
    app.receivedEvent('deviceready') 
    }, 
    receivedEvent: function (id) { 
    console.log('Received Event: ' + id) 
    } 
} 

app.initialize() 

src/main.js

import Vue from 'vue' 
import App from './App' 

/* eslint-disable no-new */ 
const app = new Vue({ 
    template: '<App/>', 
    components: { App } 
}) 

app.$mount('#app') 

配置/ index.js:

module.exports = { 
    build: { 
    env: require('./prod.env'), 
    index: path.resolve(__dirname, '../dist/index.html'), 
    assetsRoot: path.resolve(__dirname, '../dist'), 
    assetsSubDirectory: 'static', 
    //assetsPublicPath: '/', 
    productionSourceMap: true, 
    productionGzip: false, 
    productionGzipExtensions: ['js', 'css'] 
    }, 
    dev: { 
    env: require('./dev.env'), 
    port: 8080, 
    assetsSubDirectory: 'static', 
    assetsPublicPath: '/', 
    proxyTable: {}, 
    cssSourceMap: false 
    } 
} 

webpack.base.conf.js:

module.exports = { 
    entry: { 
    app: './src/main.js' 
    }, 
    output: { 
    path: config.build.assetsRoot, 
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath, 
    filename: '[name].js' 
    }, 
    resolve: { 
    extensions: ['', '.js', '.vue', '.json'], 
    fallback: [path.join(__dirname, '../node_modules')], 
    alias: { 
     'vue$': 'vue/dist/vue.common.js', 
     'src': path.resolve(__dirname, '../src'), 
     'assets': path.resolve(__dirname, '../src/assets'), 
     'components': path.resolve(__dirname, '../src/components'), 
     'semantic': path.resolve(__dirname, '../node_modules/semantic-ui-css/semantic.min.js') 
    } 
    }, 
    resolveLoader: { 
    fallback: [path.join(__dirname, '../node_modules')] 
    }, 
    plugins: [ 
    new webpack.ProvidePlugin({ 
     // jquery 
     $: 'jquery', 
     jQuery: 'jquery', 
     'window.jQuery': 'jquery', 
     semantic: 'semantic-ui-css', 
     'semantic-ui': 'semantic-ui-css' 
    }) 
    ], 
    module: { 
    preLoaders: [ 
     { 
     test: /\.vue$/, 
     loader: 'eslint', 
     include: projectRoot, 
     exclude: /node_modules/ 
     }, 
     { 
     test: /\.js$/, 
     loader: 'eslint', 
     include: projectRoot, 
     exclude: /node_modules/ 
     } 
    ], 
    loaders: [ 
     { 
     test: /\.vue$/, 
     loader: 'vue' 
     }, 
     { 
     test: /\.js$/, 
     loader: 'babel', 
     include: projectRoot, 
     exclude: /node_modules/ 
     }, 
     { 
     test: /\.json$/, 
     loader: 'json' 
     }, 
     { 
     test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, 
     loader: 'url', 
     query: { 
      limit: 10000, 
      name: utils.assetsPath('img/[name].[hash:7].[ext]') 
     } 
     }, 
     { 
     test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, 
     loader: 'url', 
     query: { 
      limit: 10000, 
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]') 
     } 
     } 
    ] 
    }, 
    eslint: { 
    formatter: require('eslint-friendly-formatter') 
    }, 
    vue: { 
    loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }), 
    postcss: [ 
     require('autoprefixer')({ 
     browsers: ['last 2 versions'] 
     }) 
    ] 
    } 
} 

webpack.prod.conf:

var webpackConfig = merge(baseWebpackConfig, { 
    module: { 
    loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true }) 
    }, 
    devtool: config.build.productionSourceMap ? '#source-map' : false, 
    output: { 
    path: config.build.assetsRoot, 
    filename: utils.assetsPath('js/[name].[chunkhash].js'), 
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') 
    }, 
    vue: { 
    loaders: utils.cssLoaders({ 
     sourceMap: config.build.productionSourceMap, 
     extract: true 
    }) 
    }, 
    plugins: [ 
    // http://vuejs.github.io/vue-loader/en/workflow/production.html 
    new webpack.DefinePlugin({ 
     'process.env': env 
    }), 
    new webpack.optimize.UglifyJsPlugin({ 
     compress: { 
     warnings: false 
     } 
    }), 
    new webpack.optimize.OccurrenceOrderPlugin(), 
    // extract css into its own file 
    new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')), 
    // generate dist index.html with correct asset hash for caching. 
    // you can customize output by editing /index.html 
    // see https://github.com/ampedandwired/html-webpack-plugin 
    new HtmlWebpackPlugin({ 
     filename: process.env.NODE_ENV === 'testing' 
     ? 'index.html' 
     : config.build.index, 
     template: 'index.html', 
     inject: true, 
     minify: { 
     removeComments: true, 
     collapseWhitespace: true, 
     removeAttributeQuotes: true 
     // more options: 
     // https://github.com/kangax/html-minifier#options-quick-reference 
     }, 
     // necessary to consistently work with multiple chunks via CommonsChunkPlugin 
     chunksSortMode: 'dependency' 
    }), 
    // split vendor js into its own file 
    new webpack.optimize.CommonsChunkPlugin({ 
     name: 'vendor', 
     minChunks: function (module, count) { 
     // any required modules inside node_modules are extracted to vendor 
     return (
      module.resource && 
      /\.js$/.test(module.resource) && 
      module.resource.indexOf(
      path.join(__dirname, '../node_modules') 
     ) === 0 
     ) 
     } 
    }), 
    // extract webpack runtime and module manifest to its own file in order to 
    // prevent vendor hash from being updated whenever app bundle is updated 
    new webpack.optimize.CommonsChunkPlugin({ 
     name: 'manifest', 
     chunks: ['vendor'] 
    }) 
    ] 
}) 

if (config.build.productionGzip) { 
    var CompressionWebpackPlugin = require('compression-webpack-plugin') 

    webpackConfig.plugins.push(
    new CompressionWebpackPlugin({ 
     asset: '[path].gz[query]', 
     algorithm: 'gzip', 
     test: new RegExp(
     '\\.(' + 
     config.build.productionGzipExtensions.join('|') + 
     ')$' 
    ), 
     threshold: 10240, 
     minRatio: 0.8 
    }) 
) 
} 

這是我的項目結構:

app structure

什麼768,16我做的就是從科爾多瓦到我的Vue組件的事件?

+0

您收到了哪些錯誤?這兩個「應用」變量是否相互衝突? –

+0

我沒有收到任何錯誤,但沒有收到'deviceready'事件。我認爲我應該做的是,當我收到'deviceready'事件時,我實例化了我的Vue應用程序,但事件從未觸發 –

+0

是否將index.js或main.js加載到頁面中?你是在物理設備上還是在瀏覽器上運行它(你需要在設備上使用cordova編譯過程來包含cordova.js)。試着移動你的科爾多瓦初始化代碼,如果你知道這一點,可以使用IDK,但是你的科爾多瓦初始化代碼現在只打印到控制檯(不初始化Vue)。另外,一般來說,將所有app/index/main命名都是一個好主意。 –

回答

2

看起來問題是Cordova.js不包含在瀏覽器中,您需要在物理設備上進行測試。

這並不意味着你不能在瀏覽器中創建原型。在JS中,如果cordova已經加載,window.cordova將被定義。所以,你可以設置你初始化像這樣

if(window.cordova){ 
    //add deviceready event to start app 
} else { 
    //call starting function 
} 

對於需要一個物理設備的事情:振動,加速度計,等等,你必須建立並部署到設備,瀏覽器本身是不夠的。

+0

看起來像cordova沒有加載構建(鉻遠程devtools告訴我,window.cordova沒有設置),添加webpack配置問題,因爲它可能是相關的 –

+0

'window.cordova'將在設備上定義,其中cordova構建過程包括'cordova.js'文件。它會在缺少'cordova.js'的瀏覽器中定義。您可以檢查它是否已定義,併爲每個實例創建編程路徑,如果已定義deviceready,則等待deviceready,如果未定義則準備好文檔。 –

+0

這是否解決了@WilliamD問題? –

2

看到這個ARTICAL,它的工作對我來說:

https://coligo.io/building-a-mobile-app-with-cordova-vuejs/

要允許應用訪問Vue.js庫,我們還需要添加以下的內容安全策略的結尾( CSP)元標記www/index.html:

; script-src 'self' http://cdn.jsdelivr.net/vue/1.0.16/vue.js 'unsafe-eval'