vue v2,vue-router和cordova

科尔多瓦 Vue.js

LGil泡芙

2020-03-12

编辑

因此,我发现这与路由器处于历史记录模式有关,如果我'mode': 'history',从router.js中删除,一切将再次正常!如果其他人有同样的问题,或者有人可以提供解释,请离开这里。

原版的

我无法将vue v2与vue-router和cordova一起使用(即cordova/www从index.html文件构建并让cordova工作)。我曾经能够使用vue和vue-router v1。我也可以使用vue v2,但无需使用vue-router。

需要明确的是,该应用程序npm run dev仅在不使用build时才可以使用index.html

我觉得这与路由器正在寻找/但看到的路径有关index.html吗?

这是您可以重现问题的存储库。

以下是一些相关代码:

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)
})

应用程序

<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
  }
}

第974篇《vue v2,vue-router和cordova》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Davaid番长十三 2020.03.12

您可能是从磁盘(“ file://”)加载文件,而从“ file://”加载文件时,浏览器历史记录API pushstate不起作用。当您从路由器中删除“模式:历史记录”时,它可以工作,因为它默认使用散列。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android