Loading external stylesheets in Nuxt 3: how to do it?
P粉905144514
P粉905144514 2023-12-20 14:54:49
0
1
586

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粉905144514
P粉905144514

reply all(1)
P粉986028039

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' }]
    }
  }
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template