javascript - How to access global mixin's methods in TypeScript Vue component? - Stack Overflow

I'm developing a Vue app using TypeScript. I created a mixin (shown in global.mixin.js below), and

I'm developing a Vue app using TypeScript. I created a mixin (shown in global.mixin.js below), and registered it with Vue.mixin() (shown in main.ts below).

global.mixin.js

import { mathHttp, engHttp } from '@/mon/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

I registered the global mixin with Vue.mixin():

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

Vue.mixin(globalMixins)

But when I try to access the mixin method from within a Vue ponent, I get an error:

property wechatShare doesn't exist on type Test.vue

Test.vue

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

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

  created() {
    this.setWeChatShare()
  }

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

How can I solve this problem?

I'm developing a Vue app using TypeScript. I created a mixin (shown in global.mixin.js below), and registered it with Vue.mixin() (shown in main.ts below).

global.mixin.js

import { mathHttp, engHttp } from '@/mon/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

I registered the global mixin with Vue.mixin():

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

Vue.mixin(globalMixins)

But when I try to access the mixin method from within a Vue ponent, I get an error:

property wechatShare doesn't exist on type Test.vue

Test.vue

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

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

  created() {
    this.setWeChatShare()
  }

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

How can I solve this problem?

Share Improve this question edited Dec 10, 2019 at 8:52 tony19 139k23 gold badges278 silver badges348 bronze badges asked Dec 10, 2019 at 4:50 ChenLeeChenLee 1,5195 gold badges18 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

vue-property-decorator uses the same semantics for mixins from vue-class-ponent. Based on the example from vue-class-ponent docs, the mixin takes the same form as a ponent:

src/mixin.ts:

import Vue from 'vue'
import Component from 'vue-class-ponent'

@Component
export default class MyMixin extends Vue {
  wechatShare(config) {
    //...
  }
}

Using the Mixins from vue-property-decorator (or mixins from vue-class-ponent), wrap your custom mixin, and extend it with your ponent:

src/App.vue:

import { Component, Mixins } from 'vue-property-decorator'
// OR
// import Component, { mixins } from 'vue-class-ponent'

import MyMixin from './mixin'

@Component
export default class App extends Mixins(MyMixin) {
  mounted() {
    this.wechatShare(/* ... */)
  }
}

For those who want to use mixin globally and prevent importing in every ponent, this is what you can do.

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
    }

inside src/main.ts

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

inside your 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
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492522a4630048.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信