2.3.3
https://github.com/lucastheisen/vue-instance-method-fail
export class AppVuePlugin {
public static install(vue: typeof Vue, options: any) {
Object.defineProperties(vue.prototype, {
$api: {
get() { return new AppApi() },
},
})
}
}
Vue.use(AppVuePlugin);
@Component({})
export default class AppComponent extends Vue {
public data: AppData
public created() {
this.data = this.$api.getAppData(); // <- Property '$api' does not exist on type 'AppData'
}
I expect the error but would like to see an official way to allow for this...
The ts build error:
ERROR in C:\Users\ltheisen\git\pastdev-vue-instance-method-fail\src\app.vue.ts
(33,22): error TS2339: Property '$api' does not exist on type 'App'.
Not sure the best approach for this, perhaps allowing some sort of generic on the Vue class? Or an official documented suggestion for working around this... Right now i create a subclass, but it feels wrong:
import Vue from 'vue'
export default class AppVue extends Vue {
public $api: AppApi
}
And then use this _version_ of Vue everywhere else:
import Vue from './app-vue'
You can just augment Vue's types. See: https://github.com/vuejs/vue/blob/dev/types/test/augmentation-test.ts
@lucastheisen This post can help you also:
https://stackoverflow.com/questions/43142304/how-to-augment-the-vue-class-and-keep-typescript-definition-in-sync/43232151#43232151
@gnumarcelo This code worked for me only when I put it in the same plugin file..
Here is my axio-plugin.ts file:
import _Vue from 'vue';
import Axios from 'axios';
export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void {
// do stuff with options
Vue.prototype.$http = Axios;
}
export class AxiosPluginOptions {
// add stuff
}
import { AxiosStatic } from 'axios';
declare module 'vue/types/vue' {
interface Vue {
$http: AxiosStatic;
}
}
@gnumarcelo This code worked for me only when I put it in the same plugin file..
Here is my axio-plugin.ts file:
import _Vue from 'vue'; import Axios from 'axios'; export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void { // do stuff with options Vue.prototype.$http = Axios; } export class AxiosPluginOptions { // add stuff } import { AxiosStatic } from 'axios'; declare module 'vue/types/vue' { interface Vue { $http: AxiosStatic; } }讗讞 砖诇讬 住讬讚专转 讗讜转讬 讘讜诪讘讛
(some thank you words in Hebrew)
Most helpful comment
@gnumarcelo This code worked for me only when I put it in the same plugin file..
Here is my axio-plugin.ts file: