admin管理员组文章数量:1427586
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 badges2 Answers
Reset to default 2vue-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
}
本文标签: javascriptHow to access global mixin39s methods in TypeScript Vue componentStack Overflow
版权声明:本文标题:javascript - How to access global mixin's methods in TypeScript Vue component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745492522a2660664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论