DEV Community

Cover image for How to use Ionicons v5 with Vue.js

How to use Ionicons v5 with Vue.js

Andreas on February 11, 2020

There are a lot of awesome icon sets out there, but the set from the Ionic framework - Ionicons - is still one of my favorites. It surely is a matt...
Collapse
 
njt1982 profile image
Nicholas Thompson

If you're using interiajs or something else with vite, I had to do this:

export default defineConfig({
    plugins: [
        vue({
            template: {
              compilerOptions: {
                isCustomElement: (tag) => tag.startsWith('ion-')
              },
            },
        }),
    ],
});
Enter fullscreen mode Exit fullscreen mode

Hopefully useful for someone :)

Collapse
 
ryansmith profile image
Ryan Smith

I believe that is the current recommended approach, but I agree that it isn't as clean as it could be. A Vue component to wrap the Ionicons component might make it feel more integrated, but I have not done that before. This approach also works with any Web Component, not just Ionicons.

Ionic is working on a way to have Stencil output targets with framework bindings because including Web Components in frameworks can be awkward, so that will streamline the process a little more and make them seem more like they belong in the framework app. Unfortunately, the Vue version has not had progress on it compared to Angular and React, but it is on their list.

Collapse
 
devmount profile image
Andreas • Edited

Thank you very much for this detailed statement! Glad to know that I'm not doing something weird 😅 Let's hope that there will be some progress for Vue soon.

Collapse
 
zermattchris profile image
Chris Banford

Had to tweak the module import to:

<script src="https://unpkg.com/ionicons@5.0.0/dist/ionicons.js"></script>

As per the project's instructions:
github.com/ionic-team/ionicons

Hope this helps someone save a couple of minutes :-)

Collapse
 
carlflor profile image
Carl Flor

Doesn't this give you a deprecation warning?

github.com/ionic-team/ionicons/iss...

Collapse
 
maprangsoft profile image
Maprangsoft

thank you very much.

Collapse
 
devmount profile image
Andreas

You're welcome 😊

Collapse
 
abdusamikovna profile image
Durdona

vue-ionicons can be a solution to use it as a component. Please have a look github.com/mazipan/vue-ionicons

Collapse
 
devmount profile image
Andreas

Many thanks for this recommendation. As far as I see this isn't compatible with Ionicons v5.x, unfortunately.

Collapse
 
dasper profile image
Will Shaw

Would it be possible to get this updated for Vue3? I am trying to figure this out in using Vite and typescript and keep coming up at a loss.

Collapse
 
devmount profile image
Andreas

Should be similar in Vue3, except ignoredElements is now isCustomElement. Also you need to distiguish between on-the-fly compilation and pre-compilation. This page in the docs should help you.

On-the-fly example:

const app = Vue.createApp({});
app.config.isCustomElement = tag => tag.startsWith('ion-');
Enter fullscreen mode Exit fullscreen mode

Webpack example in vue.config.js:

module.exports = {
    chainWebpack: config => {
        config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
            options.compilerOptions = {
                ...(options.compilerOptions || {}),
                isCustomElement: tag => /^ion-/.test(tag)
            };
            return options;
        });
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
k3nnie profile image
k3nnie

Thank You for this article - I really like how you can customize the new Ionicons.

Collapse
 
devmount profile image
Andreas

My pleasure! 😊