Vue router and Cordova (Vue.js)
Asked Answered
B

5

36

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,
  }
}
Brendin answered 30/11, 2016 at 22:58 Comment(1)
Try setting assetsPublicPath: './' in your build block in config/index.js. I have Vue2 Cordova apps using vue-router working, so it definitely is possibleParmenter
F
15

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.

Fibrilla answered 17/2, 2017 at 14:43 Comment(1)
There is a better solution as of cordova-ios 6 released june 2020, it can now be served using app://localhost/ on iOS and android defaults to serve using localhostIndelicate
D
4

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

Dilks answered 4/7, 2017 at 14:32 Comment(0)
H
3

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

Hartley answered 8/4, 2018 at 13:45 Comment(0)
P
0

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

Prelacy answered 20/12, 2017 at 8:10 Comment(0)
B
0

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.

Brunildabruning answered 20/5, 2019 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.