Vuejs,使用相对路径构建的困难

新手上路,请多包涵

当我运行 npm run build 时,我在使用相对路径进行正确构建时遇到了困难。解析资产很容易,但我不知道如何配置两件事:

1/ assetsPublicPathconfig/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, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/ONLY_ABSOLUTE_PATH_ARE_ALLOWED/',
    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
  }
}

2/ vue-router 配置中的 base 选项似乎也只接受绝对路径…

 const router = new VueRouter({
  mode: 'history',
  base: '/ABSOLUTE_PATH_ONLY/',
  routes: [
    { path: '/', redirect: '/fr/poster/home' },
    { path: '/:lang', redirect: '/:lang/poster/home' },
    {
      path: '/:lang/poster',
      component: PosterLayout,
      children: [
        {
          // UserProfile will be rendered inside User's <router-view>
          // when /user/:id/profile is matched
          name: 'home',
          path: 'home',
          component: Home
        },
        {
          // UserPosts will be rendered inside User's <router-view>
          // when /user/:id/posts is matched
          name: 'themeCover',
          path: ':theme',
          component: ThemeCover
        }
      ]
    },
    {
      name: 'themeDetails',
      path: '/:lang/theme/:theme',
      component: ThemeDetails
    }
  ]
})

因此,当我指定正确的未来 URL 时它可以工作,但它不是“未来证明”,以防服务器被修改……

如果可以解决,有什么想法可以解决这个问题吗?

原文由 Existe Deja 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 704
2 个回答

我知道从你写这篇文章开始一切都变了。但是此时使用最新版本的 Vue 和 Vue Cli,您可以通过 vue 配置文件获得它(我不是这个平台的专家):

  1. 在项目的主路径创建一个“vue.config.js”文件

  2. 给出一个相对路径。例子:

     module.exports = {
        publicPath: './'
    };

它不适用于在 css 中添加的字体,我不知道为什么,我仍在谷歌搜索。如果有人阅读可以提供帮助,那就太好了。

原文由 J.S.R - Silicornio 发布,翻译遵循 CC BY-SA 4.0 许可协议

我用以下 vue.config.js 设置解决了我的问题:

 module.exports = {
    publicPath: process.env.BASE_URL,
    assetsDir: process.env.BASE_URL
};

我认为您可以通过更改 webpack.config.js 以及更改 output.publicPath 来做同样的事情。参考: https ://cli.vuejs.org/guide/html-and-static-assets.html#url-transform-rules

你也可以做 publicPath: process.env.BASE_URL + '/static/'

原文由 ADM-IT 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题