【问题标题】:How to access global mixin's methods in TypeScript Vue component?如何在 TypeScript Vue 组件中访问全局 mixin 的方法?
【发布时间】:2019-12-10 04:50:22
【问题描述】:

我正在使用 TypeScript 开发一个 Vue 应用程序。我创建了一个 mixin(如下面的global.mixin.js 所示),并使用Vue.mixin() 注册了它(如下面的main.ts 所示)。

global.mixin.js

import { mathHttp, engHttp } from '@/common/js/http'

export default {
  methods: {
    wechatShare(config) {
      config.imgUrl = config.imgUrl
      mathHttp.get('/wechat/config', {
        url: encodeURIComponent(window.location.href),
      }).then((data) => {
        wx.config({
          debug: false,
          appId: data.appId,
          timestamp: data.timestamp,
          nonceStr: data.noncestr,
          signature: data.signature,
          jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
        })
      })
      wx.ready(() => {
        wx.updateAppMessageShareData(config)
        wx.updateTimelineShareData(config)
      })
    },
  },
}

main.ts

我用Vue.mixin()注册了全局mixin:

import globalMixins from './mixins/global.mixin'

Vue.mixin(globalMixins)

但是当我尝试从 Vue 组件中访问 mixin 方法时,我得到一个错误:

property wechatShare doesn't exist on type Test.vue

Test.vue

<script lang='ts'>
import { Component, Prop, Vue } from 'vue-property-decorator'

@Component({ components: { } })
export default class Test extends Vue {

  created() {
    this.setWeChatShare()
  }

  setWeChatShare() {
    this.wechatShare
  }
}
</script>

我该如何解决这个问题?

【问题讨论】:

    标签: javascript typescript vue.js vuejs2


    【解决方案1】:

    vue-property-decorator 使用与vue-class-component 相同的semantics for mixins。根据vue-class-component docs 中的示例,mixin 采用与组件相同的形式:

    src/mixin.ts:

    import Vue from 'vue'
    import Component from 'vue-class-component'
    
    @Component
    export default class MyMixin extends Vue {
      wechatShare(config) {
        //...
      }
    }
    

    使用来自vue-property-decoratorMixins(或来自vue-class-componentmixins),包装您的自定义mixin,并使用您的组件扩展它:

    src/App.vue:

    import { Component, Mixins } from 'vue-property-decorator'
    // OR
    // import Component, { mixins } from 'vue-class-component'
    
    import MyMixin from './mixin'
    
    @Component
    export default class App extends Mixins(MyMixin) {
      mounted() {
        this.wechatShare(/* ... */)
      }
    }
    

    【讨论】:

      【解决方案2】:

      对于那些想要全局使用 mixin 并防止在每个组件中导入的人,这是你可以做的。

      src/mixins/mixin.ts

          import { Vue, Component } from 'vue-property-decorator'
          import Colors from "@/values/Colors"
          import Strings from "@/values/Strings";
      
          @Component
          export default class Values extends Vue {
              public test = 'Hello, hello, hello';
              public colors: {} = Colors.light;
              public strings: {} = Strings.pt
          }
      

      在 src/main.ts 中

      import Values from "@/values/Values";//my Mixin
      Vue.mixin(Values)
      

      在您的 src/shims-tsx.d.ts 中

      // add the variables,functions ... inside the vue interface and then you will good to use them anywhere. 
      interface Vue {
              colors,
            strings
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-04
        • 2017-02-06
        • 2017-12-19
        • 1970-01-01
        • 2020-09-22
        • 2021-10-20
        • 2019-09-10
        • 1970-01-01
        相关资源
        最近更新 更多