33
votes

EDIT

So I just found out it has to do with the router being in history mode, if I remove 'mode': 'history', from router.js everything works again! Leaving here if others have the same problem, or if someone can provide an explanation...

Original

I'm not able to use vue v2 with vue-router and cordova (i.e. building to cordova/www and having cordova work from the index.html file). I used to be able to with vue and vue-router v1. I'm also able to with vue v2 but without using vue-router.

To be clear, the app works when using npm run dev just not when opening the built index.html.

I have a feeling this has to do with the router looking for a path of / but seeing index.html?

Here's a repo where you can reproduce the problem.

Below is some relevant code:

main.js

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

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  // replace the content of <div id="app"></div> with App
  render: h => h(App)
})

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view></router-view>
  </div>
</template>

<script>
import Hello from './components/Hello'

export default {
  name: 'app',
  components: {
    Hello
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

/router/router.js

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Hello from '../components/Hello'

export default new Router({
  'mode': 'history',
  scrollBehavior: () => ({ y: 0 }),
  'routes': [
    {
      'path': '/',
      'component': Hello
    }
  ]
})

config/index.js

// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')

module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../../cordova/www/index.html'),
    assetsRoot: path.resolve(__dirname, '../../cordova/www'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '',
    productionSourceMap: true,
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css']
  },
  dev: {
    env: require('./dev.env'),
    port: 8080,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {},
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }
}
5
Try setting assetsPublicPath: './' in your build block in config/index.js. I have Vue2 Cordova apps using vue-router working, so it definitely is possibletoast38coza

5 Answers

15
votes

You're probably loading the files from disk ("file://") and the browser history API pushstate doesn't work when files are loaded from "file://". It works when you remove "mode: history" from the router because it defaults to using hashes.

4
votes

Set build: assetsPublicPath: 'file:///android_asset/www/'

3
votes

you have to change in index.js assetsPublicPath: '/' to assetsPublicPath: './'

0
votes

Add <base href='./'> element in your index.html head. In your build config index.js set assetsPublicPath: ''.

0
votes

I found that I should add another home router and changed from the empty string '' to '/index.html'

and change base to

<base href='./'>

and remove the history from router as Yanis said.