简体   繁体   English

Webpack尝试从子目录加载build.js

[英]Webpack trying to load build.js from subdirectory

I'm setting up a basic vue app with vuetify/vue-router, and when loading the base url '/', everything works fine. 我正在使用vuetify / vue-router设置一个基本的vue应用程序,并且在加载基本URL'/'时,一切正常。 I can click a to /manage/products without any problem. 我可以单击一个/ manage / products,没有任何问题。

However, when loading /manage/products directly by typing in the address bar, I get this error: 但是,当通过在地址栏中键入直接加载/ manage / products时,出现此错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

It seems to want to load /manage/dist/build.js instead of /dist/build.js. 似乎要加载/manage/dist/build.js而不是/dist/build.js。

Can I change my webpack.config.js to make sure the right build.js is called? 我可以更改webpack.config.js以确保调用了正确的build.js吗?

output: {
  path: path.resolve(__dirname, './dist'),
  publicPath: '/dist/',
  filename: 'build.js'
},
resolve: {
  extensions: ['.js', '.vue'],
  alias: {
    '@' : path.resolve(__dirname, './src'),
    'vue$': 'vue/dist/vue.esm.js',
    'public': path.resolve(__dirname, './public')
  }
}

vue-router 'hash' mode works, but I would like to use 'history' mode for cleaner URLs. vue-router的“哈希”模式可以工作,但是我想对更清洁的URL使用“历史记录”模式。

For reference: I've used this template 供参考:我使用了此模板

vue init vuetifyjs/webpack-simple

EDIT: 编辑:

I've found the solution. 我找到了解决方案。 The vuetifyjs/webpack-simple template had a misconfiguration from the start. vuetifyjs / webpack-simple模板从一开始就配置错误。 Inside index.html I've changed: 在index.html内部,我进行了更改:

<script src="./dist/build.js"></script>

to

<script src="/dist/build.js"></script>

And made sure that these options were present inside webpack.config.js: 并确保在webpack.config.js中存在以下选项:

devServer: {
    historyApiFallback: true
},

vue-router 'hash' mode works, but I would like to use 'history' mode for cleaner URLs vue-router的“哈希”模式有效,但是我想对更清洁的URL使用“历史记录”模式

The point of the History API is to allow you to map DOM-generated and server-generated pages onto each other. History API的重点是允许您将DOM生成的页面和服务器生成的页面相互映射。

This means that if JavaScript fails for any reason , then the server can deliver the page instead. 这意味着如果JavaScript 由于任何原因而失败,则服务器可以代替该页面。 This means that if someone deep links to a page on your site, then the server can just deliver that page (as opposed to delivering the homepage, and then using JavaScript to mutate it into the desired page). 这意味着,如果有人深入链接到您网站上的页面,则服务器仅可以交付该页面(而不是交付主页,然后使用JavaScript将其变异为所需的页面)。

You need to write server-side code that will deliver the page. 您需要编写将提供页面的服务器端代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM