【发布时间】:2021-05-20 18:26:56
【问题描述】:
我创建了一个新的静态 Nuxt.js 项目。所有资源的 url 和路径都是绝对的,即/。有没有简单的方法来使用相对 url 和路径./?
我有几个假设(我是 Java 开发人员):
- 静态 Nuxt.js 项目不需要 Web 服务器。
-
/file.js旨在由网络服务器提供服务。
该项目的目的是在本地读取数据(json 文件)并显示处理后的数据(因此没有 Web 服务器)。我要添加精美的图表、表格等……我是不是用错了方法?
我在谷歌上搜索了很长时间,但从未见过像样的答案。
这是我的 nuxt.conf.js config.output.publicPath = './_nuxt/' 似乎完成了一半的工作。它适用于<script src="./_nuxt/936eba5.js"></script>,但不适用于<link rel="preload" href="/_nuxt/936eba5.js" as="script">
export default {
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
ssr: false,
// Target: https://go.nuxtjs.dev/config-target
target: 'static',
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'my-app',
htmlAttrs: {
lang: 'en'
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
// https://go.nuxtjs.dev/typescript
'@nuxt/typescript-build',
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/buefy
'nuxt-buefy',
],
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend (config, { isDev, isClient }) {
if (!isDev) {
// relative links, please.
config.output.publicPath = './_nuxt/'
}
return config;
}
},
// router: {
// base: ''
// }
}
编辑更多信息。
_nuxt 是/dist 中的默认路径/url。
此时我只生成了一个项目,并尝试使网址相对。
/dist
- 200.html
- README.md
- _nuxt
- favicon.ico
- index.html
- 启发
所以删除config.output.publicPath = './_nuxt/'。使用ssr: true。然后运行npm run generate
这些绝对 url/路径是生成的:
<link rel="preload" href="/_nuxt/42fc731.js" as="script">
<script src="/_nuxt/42fc731.js" defer></script>
它在 Nuxt.js Github 中被多次作为错误提出,但我从未见过真正的解决方案。
【问题讨论】:
-
你想用
<link rel="preload" href="/_nuxt/936eba5.js" as="script">实现什么? -
它是在
<head>中为我生成的。该链接是绝对的。它必须是相对的。 -
你知道文件名会变吗?这可能不是在这里做事的方式。你到底想做什么?获取一些 JSON 或其他内容?
-
我再问一遍,relative URL有什么问题?你为什么不保持原样?您应该完全可以从通常的默认入口点访问它。另外,你说的问题是什么?还有,
<head>和relative URLs与您的 JSON 初始项目想法有什么关系? -
我想使用相对 url 来包含 js/css/images 等...这是我的要求。 Web 应用程序是使用绝对 url 生成的,这是我不想要的。我提到了该应用程序的近似值,以证明我希望它是本地的,而不是使用 Web 服务器。加载 JSON 不是问题 - 只有我想要一个本地 Web 应用程序。