search

Home  >  Q&A  >  body text

Loading external stylesheets in Nuxt 3: how to do it?

I'm trying to load a mapboxgl stylesheet in a Nuxt 3.0.0-rc.8 application. Normally, for Vue projects, I manually add it to the head of the index.html page.

However, this is obviously not the approach in Nuxt 3. I tried adding it to the head and css options in the nuxt.config.ts file with no success. I did notice that when I added it to the css array, it was added to my header, but the "https://" was replaced with "_nuxt".

I know I'm missing something simple. This is my config file:

export default defineNuxtConfig({
  css: [
    '~/assets/css/main.css',
  ],
  build: {
    postcss: {
      postcssOptions: require('./postcss.config.js'),
    },
  },
  buildModules: ['@pinia/nuxt'],
  runtimeConfig: {
    treesAPIKey: '',
    public: {
      baseURL: '',
      mapToken: '',
    },
  },
  head: { link: [{ rel: 'stylesheet', href: 'https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css' }] },
});

P粉905144514P粉905144514435 days ago709

reply all(1)I'll reply

  • P粉986028039

    P粉9860280392023-12-21 10:13:17

    Use app.head instead of head.

    import { defineNuxtConfig } from 'nuxt'
    
    export default defineNuxtConfig({
      app: {
        head: {
          link: [{ rel: 'stylesheet', href: 'https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css' }]
        }
      }
    })
    

    reply
    0
  • Cancelreply