【问题标题】:Vue router and Cordova (Vue.js)Vue 路由器和 Cordova (Vue.js)
【发布时间】:2017-04-15 10:52:00
【问题描述】:

编辑

所以我刚刚发现它与路由器处于历史模式有关,如果我从 router.js 中删除 'mode': 'history', 一切都会再次运行!如果其他人有同样的问题,或者如果有人可以提供解释,请离开这里......

原创

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

需要明确的是,该应用在使用 npm run dev 时有效,但在打开内置的 index.html 时无效。

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

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

下面是一些相关代码:

ma​​in.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,
  }
}

【问题讨论】:

  • 尝试在 config/index.js 的构建块中设置 assetsPublicPath: './'。我有使用 vue-router 工作的 Vue2 Cordova 应用程序,所以这绝对是可能的

标签: vue.js cordova vuejs2 vue-router npm-run


【解决方案1】:

您可能正在从磁盘(“file://”)加载文件,当从“file://”加载文件时,浏览器历史 API pushstate 不起作用。当您从路由器中删除“模式:历史”时它会起作用,因为它默认使用哈希。

【讨论】:

  • 从 2020 年 6 月发布的 cordova-ios 6 开始,有一个更好的解决方案,现在可以在 iOS 上使用 app://localhost/ 提供服务,Android 默认使用 localhost 提供服务
【解决方案2】:

设置构建:assetsPublicPath: 'file:///android_asset/www/'

【讨论】:

    【解决方案3】:

    你必须改变 index.js assetsPublicPath: '/'assetsPublicPath: './'

    【讨论】:

      【解决方案4】:

      在 index.html 头中添加 &lt;base href='./'&gt; 元素。在您的构建配置中index.js 设置assetsPublicPath: ''

      【讨论】:

        【解决方案5】:

        发现应该再添加一个家用路由器,从空字符串''改成'/index.html'

        并将基数更改为

        <base href='./'>
        

        并按照Yanis 所说的那样从路由器中删除历史记录。

        【讨论】:

          猜你喜欢
          • 2015-12-23
          • 2018-02-07
          • 2020-12-25
          • 1970-01-01
          • 1970-01-01
          • 2016-10-20
          • 1970-01-01
          • 2017-03-28
          • 2018-08-03
          相关资源
          最近更新 更多