0

Currently I use Vue 2 with Typescript, but I can't add options to the component

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import HelloWorld from "@/components/HelloWorld.vue"; // @ is an alias to /src

@Component<Home>({
  components: {
    HelloWorld,
  },

  middleware: "yyy",
})
export default class Home extends Vue {
  mounted() {
    console.log(this.middleware);
  }
}
</script>

I got undefined!

I found 1 problem similar to this, I tried but still no result

Extend Component options

My src\shims-vue.d.ts

declare module "*.vue" {
  import Vue from "vue";
  export default Vue;
}
declare module "vue/types/vue" {
  interface Vue {
    middleware?: string;
  }
}

declare module "vue/types/options" {
  interface ComponentOptions<V extends Vue> {
    middleware?: string;
  }
}
0

1 Answer 1

1

Your type declaration is missing an import for Vue:

// shims-vue.d.ts
import Vue from 'vue' 👈

declare module "vue/types/options" {
  interface ComponentOptions<V extends Vue> {
    middleware?: string;
  }
}

To access the option from your component, use this.$options.middleware (not this.middleware):

export default class Home extends Vue {
  mounted() {           👇
    console.log(this.$options.middleware);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's for instance properties. That same page shows how to augment component options with vue/types/vue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.