N
Nuxt3w ago
Abou

How to use svg

Hey, i'd like to know how i can use svg inside nuxt (i don't use vite) :
13 Replies
kapa.ai
kapa.ai3w ago
Hi @Abou. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To use SVG files in a Nuxt project, you have several options. Here are a few approaches based on the provided information: 1. Using the nuxt-svgo module: You can use the nuxt-svgo module to automatically import and use SVGs. Place your SVG files in the assets/svg/ directory and configure the module in your nuxt.config.ts file:
svgo: { autoImportPath: '~/assets/svg/' }

svgo: { autoImportPath: '~/assets/svg/' }

Then you can use the SVGs in your components without explicitly importing them:
<template>
<SvgoBackspace class="w-7 inline" />
</template>

<template>
<SvgoBackspace class="w-7 inline" />
</template>

Stack Overflow: using the nuxt-svgo module (https://stackoverflow.com/questions/78631745) 2. Using @nuxt/icon: Nuxt provides an icon component that allows you to use a wide range of icons, including SVGs, from various icon collections. You can use it like this:
<template>
<UIcon name="i-heroicons-light-bulb" class="w-5 h-5" />
</template>

<template>
<UIcon name="i-heroicons-light-bulb" class="w-5 h-5" />
</template>

To use this method, make sure to install the @nuxt/icon module and the icon collections you need. Nuxt UI: Icon Component (https://ui.nuxt.com/components/icon) 3. Using vite-plugin-svg4vue: If you're using Vite as your build tool (which is the default for Nuxt 3), you can use the vite-plugin-svg4vue plugin. Here's how to set it up:
import { defineNuxtModule, addVitePlugin } from '@nuxt/kit'
import { svg4VuePlugin } from 'vite-plugin-svg4vue'

export default defineNuxtModule({
setup(options) {
addVitePlugin(svg4VuePlugin(options.svg4vue))
},
})

import { defineNuxtModule, addVitePlugin } from '@nuxt/kit'
import { svg4VuePlugin } from 'vite-plugin-svg4vue'

export default defineNuxtModule({
setup(options) {
addVitePlugin(svg4VuePlugin(options.svg4vue))
},
})

Nuxt Docs: Builder Examples (https://nuxt.com/docs/api/kit/builder#examples-3) 4. Using the assets directory: You can place your SVG files in the assets directory and reference them in your code using the ~/assets/ path. This method doesn't provide automatic component creation but allows you to use SVGs as regular image files. Nuxt Docs: Assets Directory (https://nuxt.com/docs/getting-started/assets#assets-directory) Remember to choose the method that best fits your project's needs and configuration. If you're not using Vite, options 1, 2, and 4 might be more suitable for your setup. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@811720203531976714> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Abou
AbouOP3w ago
@kapa.ai when i'm using svgo, i got this error inside my terminal : [Vue warn]: Component <Anonymous> is missing template or render function. at <Anonymous> at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > at <Anonymous key="/" vnode= { v_isVNode: true, v_skip: true, type: { name: 'index', setup: [Function (anonymous)], ssrRender: [Function: _sfc_ssrRender], scopeId: 'data-v-02281a80', __file: '/Users/ilies/IdeaProjects/whispr/pages/index.vue' }, props: { onVnodeUnmounted: [Function: onVnodeUnmounted], ref: RefImpl { you'll find my code in a .txt and this is my nuxt.config.ts : // https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ compatibilityDate: '2024-04-03', devtools: { enabled: false }, // activer pour le mode dev (voir le temps de load du ssr etc, voir opti) css: ['~/assets/scss/main.scss'], modules: ['@nuxt/fonts', '@nuxt/image', '@nuxtjs/device', 'nuxt-svgo'], // add Poppins font fonts: { families: [ { name: 'Poppins', provider: 'google', weights: ['400', '600', '700'], styles: ['normal'], subsets: ['latin'] } ] }, runtimeConfig: { public: { apiBaseUrl: process.env.PUBLIC_API_BASE_URL || 'http://localhost:8000' } }, vite: { css: { preprocessorOptions: { scss: { api: "modern-compiler", } } } }, svgo: { autoImportPath: '~/assets/svg', } })
Nuxt
Nuxt Configuration
Discover all the options you can use in your nuxt.config.ts file.
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the error message and the configuration you've provided, it seems you're encountering an issue with the nuxt-svgo module. The error suggests that a component is missing a template or render function. This could be related to how the SVG components are being generated or imported. Let's try to address this issue: 1. First, make sure you have the latest version of nuxt-svgo installed. You can update it by running:
npm install nuxt-svgo@latest

npm install nuxt-svgo@latest

or if you're using yarn:
yarn add nuxt-svgo@latest

yarn add nuxt-svgo@latest

2. In your nuxt.config.ts, you're using the svgo configuration correctly. However, let's try to be more explicit with the configuration:
export default defineNuxtConfig({
// ... other configurations
modules: ['nuxt-svgo'],
svgo: {
autoImportPath: '~/assets/svg/',
defaultImport: 'component'
}
})

export default defineNuxtConfig({
// ... other configurations
modules: ['nuxt-svgo'],
svgo: {
autoImportPath: '~/assets/svg/',
defaultImport: 'component'
}
})

This configuration explicitly sets the defaultImport to 'component', which should ensure that SVGs are imported as Vue components Nuxt SVGO Configuration (https://github.com/cpsoinos/nuxt-svgo#configuration). 3. Make sure your SVG files are placed in the correct directory (~/assets/svg/). 4. When using the SVG components in your Vue files, you don't need to import them explicitly. The module should handle the imports automatically. For example:
<template>
<SvgoYourSvgName />
</template>

<template>
<SvgoYourSvgName />
</template>

Replace YourSvgName with the actual name of your SVG file (without the .svg extension). 5. If you're still encountering issues, try clearing your Nuxt cache and rebuilding the project:
npx nuxt cleanup
npm run dev

npx nuxt cleanup
npm run dev

6. If the problem persists, it might be helpful to check if there are any issues with the SVG files themselves. Ensure they are valid SVGs and don't contain any problematic elements. 7. As a workaround, you could try using the SVGs as regular image files instead of components:
<template>
<img src="~/assets/svg/your-svg-file.svg" alt="SVG description" />
</template>

<template>
<img src="~/assets/svg/your-svg-file.svg" alt="SVG description" />
</template>

If none of these solutions work, it might be worth considering alternative methods for using SVGs in your Nuxt project, such as using the @nuxt/image module or manually importing SVGs as Vue components. Remember, if you continue to face issues, it would be helpful to see the content of the SVG files and the Vue components where you're trying to use them to provide more specific guidance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@811720203531976714> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Abou
AbouOP3w ago
@kapa.ai good, i've find. but why is my svg black and don't have his origin color with svgo ?
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- The issue you're experiencing with your SVGs appearing black instead of their original colors when using nuxt-svgo is a common one. This typically happens because the SVGO (SVG Optimizer) module removes color information from the SVG files by default to optimize them. Here's how you can address this: 1. You can modify the SVGO configuration in your nuxt.config.ts file to preserve the colors. Add the following to your existing SVGO configuration:
export default defineNuxtConfig({
// ... other configurations
svgo: {
autoImportPath: '~/assets/svg/',
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
removeAttrs: {
attrs: ['fill', 'stroke']
}
}
}
}
]
}
}
})

export default defineNuxtConfig({
// ... other configurations
svgo: {
autoImportPath: '~/assets/svg/',
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
removeAttrs: {
attrs: ['fill', 'stroke']
}
}
}
}
]
}
}
})

This configuration tells SVGO not to remove fill and stroke attributes, which are often responsible for the colors in SVGs Stack Overflow: SVGO Color Preservation (https://stackoverflow.com/questions/78631745). 2. If you want more control over which attributes are preserved, you can adjust the removeAttrs plugin configuration accordingly. 3. After making these changes, rebuild your project to apply the new configuration:
npm run dev

npm run dev

or
yarn dev

yarn dev

4. If you're still having issues, you might need to check the SVG files themselves. Make sure they have the correct color information embedded in them (either through fill, stroke, or style attributes). 5. As an alternative, you can also apply colors to your SVGs using CSS. This method allows you to keep your SVGs optimized while still controlling their appearance:
<template>
<SvgoYourSvgName class="custom-svg-class" />
</template>

<style scoped>
.custom-svg-class {
fill: yourDesiredColor;
stroke: anotherColor;
}
</style>

<template>
<SvgoYourSvgName class="custom-svg-class" />
</template>

<style scoped>
.custom-svg-class {
fill: yourDesiredColor;
stroke: anotherColor;
}
</style>

Remember, the exact solution might depend on how your SVGs are structured and what kind of color information they contain originally. If you continue to face issues, it might be helpful to examine the content of a specific SVG file that's causing problems. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@811720203531976714> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Orbis
Orbis3w ago
What do you mean by using svg with nuxt? As img? Inline? As icon?
Abou
AbouOP3w ago
Hey, yeah i mean inline ^^
Orbis
Orbis3w ago
as img:
<img src="/path/to/svg />
<img src="/path/to/svg />
inline:
<svg>
<g>...</g>
</svg>
<svg>
<g>...</g>
</svg>
as icon (possible via usocss or nuxt icon, example is unocss):
<div class="i:my-custom-svg-as-icon />
<div class="i:my-custom-svg-as-icon />
or unplugin icons or as vue component:
<!-- example.vue -->
<template>
<svg>
<g>...</g>
</svg>
</tempalte>

<!-- usage -->
<script setup>
import Example from './example.vue'
</script>

<template>
<Example />
</template>
<!-- example.vue -->
<template>
<svg>
<g>...</g>
</svg>
</tempalte>

<!-- usage -->
<script setup>
import Example from './example.vue'
</script>

<template>
<Example />
</template>
sorry, this message was draft maybe just use last option? 🙂
Orbis
Orbis3w ago
Nuxt
Icon · Nuxt Modules
Icon module for Nuxt with 200,000+ ready to use icons from Iconify.
Abou
AbouOP3w ago
Sorry for the late answer. Thanks for your help, i did use the svgo module for the moment !
Want results from more Discord servers?
Add your server