Vuejs,难以用相对路径建立

Geo*_*bol 9 webpack vue.js vue-router vuejs2

当我跑步时,我正面临着使用相对路径进行正确构建的困难npm run build.解决资产很容易,但我不知道如何配置2件事:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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
    }
  ]
})
Run Code Online (Sandbox Code Playgroud)

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

如果它可以解决,任何想法来解决这个问题?

J.S*_*nio 17

我知道你写完这篇文章的一切都有所改变.但是目前使用Vue和Vue Cli的最后一个版本你可以使用vue配置文件获取它(我不是这个平台的专家):

  1. 在项目的主路径中创建"vue.config.js"文件

  2. 给出相对路径.例:

    module.exports = {
        publicPath: './'
    };
Run Code Online (Sandbox Code Playgroud)

它不适用于在CSS中添加的字体,我不知道为什么,我仍然在googlying.如果有人阅读可以帮助将是伟大的.

  • 如果不起作用,请指出您使用的 Vue 版本,以便人们可以帮助您。目前我没有使用 Vue,我不知道上一个版本是否发生了变化。 (3认同)
  • 你好,我创建了一个带有 publicPath: './' 的 vue.config.js。但是,相对路径未添加到构建中。例如: &lt;link href=css/app.a3662c38.css rel=preload as=style&gt;&lt;link href=css/chunk-vendors.d95e1e17.css rel=preload as=style&gt; 我想要添加 './'前。我正在使用@vue/cli 4.3.1。谢谢 (2认同)

ADM*_*-IT 8

我已经通过以下vue.config.js设置解决了我的问题:

module.exports = {
    publicPath: process.env.BASE_URL,
    assetsDir: process.env.BASE_URL
};
Run Code Online (Sandbox Code Playgroud)

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

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


mot*_*elu 5

绝对路径不必包含域名,只需从根开始即可。

考虑一下 HTML5 URL。例如,如果您的静态文件夹位于http://www.example.com/static且当前 url 为,http://www.example.com/users则相对路径将为../static。但是,如果您尝试查看用户的详细信息并转到http://www.example.com/users/john-doe,则相对路径将为../../static。如果您不知道资产在哪里,则无法加载它们,这就是为什么您需要一个起点,一个绝对 URL。

你害怕什么问题?你可以说得更详细点吗?

  • 好吧,需要更精确!我有一台服务器用于多个应用程序,每个应用程序在服务器上都有一个专用文件夹,例如“/builds/app-name”。我使用绝对路径构建了所有应用程序,但我们正在迁移到具有另一个架构的另一个服务器,我将不得不重建所有应用程序...我想知道是否可以使用相对路径作为起点构建应用程序。这样我就可以将我的应用程序复制粘贴到我想要的任何地方。 (2认同)