'How to import a function and apply to this within vue app
Is there a way in Typescript and Vue (v2.x) to import a function that references this?
I haven't had luck with mixins, and curious if there's a simple approach here.
For example:
getPath.ts
export default getPath(): string {
return this.$route.path; // 'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
}
App.vue
import getPath from "./getPath";
import Vue from "vue";
export default Vue.extend({
mounted(){
const path = getPath().apply(this);
}
});
I'm getting the error:
'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
Solution 1:[1]
You can use composition api useRoutePath.ts:
import { useRoute } from 'vue-router';
export default useRoutePath () {
const route = userRoute();
const getPath = () => route.path;
return {
getPath,
}
}
It already has types and can be extended any time (like get params id or meta)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | George Madiarov |
