I am working on a vue app. At the moment the app consists of a large number of components and a lot of code. Currently these components (almost all of them) do not have an id assigned to them. I would like to assign id to each and every component.
Since there are a lot of components I do not want to physically go to each component file and then give it the id attribute. Ideally I would like to assign it an ID using a function.
I have searched for a solution and have come across Vue.js : How to set a unique ID for each component instance? The solutions in this question discuss adding a plugin or a mixin that generates a unique id, but this still requires me to add the ID attribute physically in each component.
I have come up with the following function
const assignIds = (app, components = {}) => {
let tags = components;
let workingTag;
console.log(app)
console.log(app.$children.length)
workingTag = app.$options._componentTag || app.$parent.$options._componentTag || "";
let myTag = (workingTag + app.$el?.localName).toLowerCase().replaceAll('-','');
let tagVal;
let tagCount = tags[myTag] || 0;
tagVal = myTag + tagCount;
++tagCount;
tags[myTag] = tagCount;
if(app.$el && (!app.$el?.id || app.$el?.id.includes("BVID"))){
app.$el.id = tagVal;
}
if(app.$children.length > 0){
let currentTag = app.$children;
currentTag.forEach(element => {
assignIds(element, tags)
});
}
},
I am not sure where and how to add this function so that it runs on every mount in the application (multiple components have the mount being called) or add it as a plugin