I tried doing this, but it does not work.
// filter.js
export default {
converTimestamp: function (seconds) {
var date = new Date(seconds * 1000);
return date.toDateString();
}
};
// main.js
import customFilters from './store/filters';
STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.
Vue Filters differ from computed properties in two ways. Going off number 2, because VueJS filters are meant for text transformations, they can only be used in two places: mustache interpolations (the curly braces in your template) and in v-bind expressions.
There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.
Here's an example:
// MyFilter.js
import Vue from 'vue';
Vue.filter('myFilter', value => {
return value.toUpperCase();
});
// main.js
import './MyFilter.js';
If you don't want to register the filters globally, you can do it like this:
// MyFilter.js
export default function (value) {
return value.toUpperCase();
}
// MyComponent.vue
import MyFilter from './MyFilter.js';
export default {
filters: {
MyFilter,
},
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With