'Use filter in Vue3 but can't read globalProperties

Just a quick question,

I know that Vue3 doesn't use filters anymore and notes says use computed or methd instead. but also there is a globalProperties we can use, I used this globalProperties but keep getting this error

Uncaught TypeError: Cannot read property 'globalProperties' of undefined

Does anyone know where is the bug in my code?

const app = {
data() {
    return {
        message: ""
    }
  }
}

app.config.globalProperties.$filters = {
formatDate(value) {

    if (value == "0001-01-01T00:00:00")
        return "";
    var today = new Date(value);
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();

    today = dd + '/' + mm + '/' + yyyy;
    return today;
   }
}

Vue.createApp(app).mount('#app');

And I am using the filter in my table like this

    <td>
         {{ $filters.formatDate(incident.incidentData) }}
    </td>


Solution 1:[1]

The config field belongs to the root instance not to the root component so you should do:

const app = {
data() {
    return {
        message: ""
    }
  }
}
const myApp=Vue.createApp(app)

myApp.config.globalProperties.$filters = {
formatDate(value) {

    if (value == "0001-01-01T00:00:00")
        return "";
    var today = new Date(value);
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();

    today = dd + '/' + mm + '/' + yyyy;
    return today;
   }
}

myApp.mount('#app');

Vue.createApp(app) return the root instance

myApp.mount('#app'); after mounting root app to an element it returns the root component

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1