HayleeGal58
HayleeGal58

Reputation: 255

How can I import a file of filter into main.js?

Is there a way I can I have a file of filters and import them in? I tried the following below but my linter keeps throwing errors saying the imported file is not used.

main.js

import Vue from 'vue'
import App from './App.vue'

import VueRouter from 'vue-router';
import Filter from './filters.js'
Vue.use(VueRouter);

filters.js

const Filter = Vue.filter('uppercase', function() {
    return "testing"
});

export Filter;

Upvotes: 1

Views: 311

Answers (2)

Raeisi
Raeisi

Reputation: 2181

There is no need to export your filters:

import Vue from 'vue';
Vue.filter('uppercase', function() {
   return "testing"
});

and just import it:

import Vue from 'vue'
import App from './App.vue'

import VueRouter from 'vue-router';
import './filters.js'
Vue.use(VueRouter);

Upvotes: 1

علی سالمی
علی سالمی

Reputation: 397

Why you import filter but not using that? you have two options:

1- inject filter to vue instant:

import Filter from './filters.js';
Vue.prototype.filter = Filter;

then you can access to filter by this.filter in your components(like this.$router)

2- if you are using Eslint you can disable it for that line by writing an special comment above of it. more info

Upvotes: 0

Related Questions