vuejs:外部文件中的过滤器?

Wel*_*lls 6 vue.js

src/app.js 好像:

import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource';

Vue.use(VueRouter);
Vue.use(VueResource);

const app = require("./app.vue");
const f = require('./filters');

const router = new VueRouter({
    routes: [
        {name: 'home', path: '/', component: home}
    ]
});

new Vue({
    router: router,
    filters: f,
    render: h => h(app)
}).$mount("#app");
Run Code Online (Sandbox Code Playgroud)

src/filters/index.js 好像:

module.exports = {
    season: (value) => {
        return 'foo';
    }
}
Run Code Online (Sandbox Code Playgroud)

使用webpack到它卷起来,但过滤器不工作,Vue公司警告我喜欢这样:

build.js:830 [Vue warn]: Failed to resolve filter: season
(found in <Anonymous>)
Run Code Online (Sandbox Code Playgroud)

如何正确地将我的过滤器放在一个单独的文件中?

Ber*_*ert 1

您没有全局注册该过滤器,您只是将其注册为在#app. 但您的应用程序会立即呈现组件app.

要使season过滤器全局可用,请使用

Vue.filter("season", f.season)
Run Code Online (Sandbox Code Playgroud)

在你的 app.js 中。

或者您可以将过滤器导入到使用它们的组件中。