问题
i recently converted my vuejs to typescript base, but now i have problem with using mixin in typescript. i used two types of mixin, but none worked for me and it shows error ts2339, and i couldnt found the solution on https://vuejs.org/v2/guide/typescript.html#Basic-Usage: first one:
//mixin.ts
import {ViewState} from "@/types";
import {mocked_view_state} from "@/api/mock/profile.device.group.mock";
export const ProfileDeviceGroup = {
methods: {
resetViewState(): ViewState {
return mocked_view_state;
}
}
};
second one:
//mixin.ts
import Vue from "vue";
import {ViewState} from "@/types";
import {mocked_view_state} from "@/api/mock/profile.device.group.mock";
const ProfileDeviceGroup = Vue.extend({
methods: {
resetViewState(): ViewState {
return mocked_view_state;
},
}
});
export default ProfileDeviceGroup;
so, is there any way to fix this problem?
..............
Update:
..............
after using vue-typed-mixins, the problem has been solved, but another problem exposes as you can see in below picture:
回答1:
You can use a package like vue-typed-mixins or migrate to the Vue class component pattern and use implements
.
@Component
export default class ComponentWithMixins extends Vue implements MyMixin {}
If you're using vue-property-decorator
, that has built-in support for mixins as well:
@Component
export default class ComponentWithMixins extends mixins(MyMixin) {}
回答2:
based on this issue https://github.com/ktsn/vue-typed-mixins/issues/4 which i created in vue-typed-mixins's github, it is a not-fixed issue which the author believes webstorm and Vuetr plugin should fix, and its not related to vue-typed-mixins. however i think the problem still is related to vue-typed-mixins plugin, but i hope the problem will be fixed soon and should thank the author of vue-typed-mixins team, and webstorm team and Vuetr.
for those who (like myself) want to keep going with vue+typescript, there seems to be a better way to pass these problems, which is vue2-composition-api, which some teams has been using and told me it was ok with typescript and has no problem since there is no need to use mixin anymore. is vuejs2.x composition api fully compatibile with typescript?
i hope it work...
来源:https://stackoverflow.com/questions/59828104/ts2339-error-how-to-use-typescript-mixin-in-vuejs-project