简体   繁体   English

当我在 css 和 js 文件 url 上部署 nuxt static 应用程序时出错

[英]Error when I deploy a nuxt static app on css and js files urls

I develop a Nuxt static app and I have a trouble when I generate the app.我开发了一个 Nuxt static 应用程序,但在生成该应用程序时遇到了问题。 When I am using locally I don't have any problem with "npm run dev", but when I upload the code to the Amazon S3 bucket or use "npm run start" in my routes load the incorrect url for the js and css files for example: Locally for style.css file the app load "/css/style.css" this URL is correct but for another route I need the same url but the app create another url for the css or js files and don't found it, the incorrect url is for example "/xxxxxxx/css/style.css", Does anyone have an idea why this happens?当我在本地使用时,“npm run dev”没有任何问题,但是当我将代码上传到 Amazon S3 存储桶或在我的路由中使用“npm run start”时,为 js 和 css 文件加载不正确的 url例如:在本地为 style.css 文件应用程序加载“/css/style.css”这个 URL 是正确的但是对于另一条路线我需要相同的 url 但是应用程序为 css 或 js 文件创建另一个 url 但没有找到它,不正确的 url 例如是“/xxxxxxx/css/style.css”,有人知道为什么会这样吗?

the app structure is:应用程序结构是:

/assets
/components
/css
/js
/images
/layouts
/pages
   /index.js -->"here doesn't have any problem"
   /preguntasFrecuentes.js  -->"Here is the problem"
/static
...

This is my nuxt.config.js :这是我的nuxt.config.js

export default {
  target: 'static',
  server: {
    host: '0.0.0.0', // default: localhost
    port: 3000, // default: 3000
  },
  components: true,
  head: {
    title: 'SirFunding',
    meta: [
      { charset: 'utf-8' },
      {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0',
      },
      {
        hid: 'description',
        name: 'description',
        content: 'my website description',
      },
      { hid: 'X-UA-Compatible', content: 'IE=edge' },
      { name: 'facebook-domain-verification', content: 'm87opl945q79izhvirpbahxeb8qmbb' },
    ],
    link: [
      { href: 'css/bootstrap.css', rel: 'stylesheet' },
      { href: 'css/style.css', rel: 'stylesheet' },
      { href: 'css/responsive.css', rel: 'stylesheet' },
      { href: 'https://fonts.googleapis.com/css?family=Barlow Semi Condensed', rel: 'stylesheet' },
      { href: 'css/color-switcher-design.css', rel: 'stylesheet' },
      { rel: 'shortcut icon', href: 'images/icono.png', type: 'image/x-icon' },
      { rel: 'icon', href: 'images/icono.png', type: 'image/x-icon' },
    ],
  },
  modules: ['@nuxtjs/axios'],
  buildModules: ['@nuxtjs/moment'],
}

and when I start the app with npm run start or upload in production the app looks like this:当我使用 npm run start 或 upload in production 启动应用程序时,应用程序如下所示:

该图像显示了没有 css 和 js 文件的应用程序

but when I start the app with npm run dev the app looks like this:但是当我使用 npm run dev 启动应用程序时,应用程序看起来像这样:

该图像显示了 css 和 js 文件的外观

the problem I found is when I build the app their paths for the css and js files are created incorrectly like this:我发现的问题是,当我构建应用程序时,它们的 css 路径和 js 文件创建不正确,如下所示:

控制台部分的图像显示 css 和 js 文件的错误 url

in production o after to build the app adds in css or js files the name of the route in the url like this: http://192.168.0.15:3000/**preguntasFrecuentes**/js/color-settings.js or http://192.168.0.15:3000/**preguntasFrecuentes**/css/color-switcher-design.css and it is incorrect, the correct URL for both URLs are whitout preguntasFrecuentes .在生产 o 之后构建应用程序添加 css 或 js 文件 url 中的路由名称,如下所示: http://192.168.0.15:3000/**preguntasFrecuentes**/js/color-settings.jshttp ://192.168.0.15:3000/**preguntasFrecuentes**/css/color-switcher-design.css不正确,两个 URL 的正确 URL 都没有preguntasFrecuentes I try moving the css and js files but this does not solve the problem我尝试移动 css 和 js 文件,但这并不能解决问题

the preguntasFrecuentes.vue code is: preguntasFrecuentes.vue代码是:

<template>
  <div class="p-3 m-3 color_title1">
    <div class="p-2 col-sm-12 todos_text">
      <h2 class="text-center text-dark">Preguntas Frecuentes</h2>
    </div>
    <div>
      <div class="text-justify col col-12">
        <div id="preguntasfrecuentesAccordion" class="accordion">
          <SectionsFaqCard v-for="faq in faqs" :key="faq.id" :faq="faq"> </SectionsFaqCard>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import faq from '~/static/FAQ.json'

export default {
  data() {
    return {
      faqs: {},
    }
  },
  created() {
    this.faqs = faq.FAQ
  },
}
</script>

I want to comment that I found the solution for this issue and it was very simple only I added "/" in nuxt.confing.js in link part in the begining of the url like this:我想评论一下,我找到了这个问题的解决方案,它非常简单,只是我在 url 开头的链接部分的 nuxt.confing.js 中添加了“/”,如下所示:

export default {
    ...,
    link: [
       { href: '/css/bootstrap.css', rel: 'stylesheet' },
       { href: '/css/style.css', rel: 'stylesheet' },
       { href: '/css/responsive.css', rel: 'stylesheet' },
       { href: 'https://fonts.googleapis.com/css?family=Barlow Semi Condensed', rel: 'stylesheet' },
       { href: '/css/color-switcher-design.css', rel: 'stylesheet' },
       { rel: 'shortcut icon', href: 'images/icono.png', type: 'image/x-icon' },
    ],
  },
  ....
}

and for the js files I add the same '/' in the app.html file.对于 js 文件,我在 app.html 文件中添加了相同的“/”。

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

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