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,
}
}
Vue Router is the official library for page navigation in Vue applications. Vue Router is simple to use, yet powerful.
SPAs are great because they don't require page loads every time the route changes. This means that once everything is loaded, we can switch the view really quickly and provide a great user experience. If you want to build a SPA in Vue, you're going to need Vue Router.
Vue router: Vue Router helps link between the browser's URL/History and Vue's components allowing for certain paths to render whatever view is associated with it. A vue router is used in building single-page applications (SPA). The vue-router can be set up by default while creating your new project.
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.
Set build: assetsPublicPath: 'file:///android_asset/www/'
you have to change in index.js
assetsPublicPath: '/'
to
assetsPublicPath: './'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With