简体   繁体   English

VueJS:将mixin应用于异步组件

[英]VueJS: apply mixin to async component

I am using Webpack 2 and importing my components via special require syntax. 我正在使用Webpack 2并通过特殊的require语法导入组件。

There are over 100 components, but only 5-10 used at a time. 有超过100个组件,但一次仅使用5-10个组件。 Most of them (but not all) partially have same functionality like props and lifecycle hooks. 其中大多数(但不是全部)部分具有相同的功能,例如道具和生命周期挂钩。

Here is code: 这是代码:

// app.js
...

Vue.component("foo", resolve => {
  require(['./components/foo.vue'], resolve);
});

...

I want to apply mixin to async component, but how to do that? 我想将mixin应用于异步组件,但是该怎么做呢? Global mixin apply to all components, but that's not what I need. Global mixin适用于所有组件,但这不是我所需要的。

I found that feature request , but it closed. 我找到了该功能请求 ,但已关闭。

I found some creepy(?) solution, but it works : 我找到了一些令人毛骨悚然的解决方案,但是它可以正常工作

// mixins.js
export default class Mixins {
   static fooMixin() {
     return {
       created: function () {
         console.log('mixin hook called');
       }
     }
   }
}

// app.js
Vue.component("foo", resolve => {
  require(['./components/foo.vue'], resolve);
});

// foo.vue
<script>
  import Mixins from "mixins";

  export default {
    ...
    mixins: [Mixins.fooMixin()]
  }
</script>

But I hope that there is a more elegant solution. 但我希望有一个更优雅的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM