Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Can i use mixins global in vue? i am use typescript with vue #226

Closed
cuongdevjs opened this issue Jul 23, 2019 · 5 comments
Closed

Can i use mixins global in vue? i am use typescript with vue #226

cuongdevjs opened this issue Jul 23, 2019 · 5 comments

Comments

@cuongdevjs
Copy link

cuongdevjs commented Jul 23, 2019

mounted() {
    Vue.$notify("OK", "success", "notify", 2000);
  }

if i call function $notify (in file mixins and declared in main.ts) by this or Vue. ts will show error this function doesn't exist in module. So how to can call this function in the component?

@bestickley
Copy link

I have this same question. I've tried augmenting types shown here: https://vuejs.org/v2/guide/typescript.html#Augmenting-Types-for-Use-with-Plugins
But still no luck.

@bestickley
Copy link

bestickley commented Jul 26, 2019

Solution:

//inside src/mixins.ts
import { Component, Vue } from "vue-property-decorator";

@Component
export default class Mixin extends Vue {
  toCurrency(number: number) {
      const formatter = new Intl.NumberFormat("en-US", {
        style: "currency",
        currency: "USD",
        minimumFractionDigits: 2
      });
      return formatter.format(number);
    }
}
//inside src/main.ts
import mixins from "@/mixins";
Vue.mixin(mixins)
// inside src/types.d.ts
import Vue from "vue";
declare module "vue/types/vue" {
  interface Vue {
    toCurrency(number: number): string;
  }
}
// inside src/App.vue\
const amount = this.toCurrency(1000);

@cuongdevjs
Copy link
Author

Can you sure it work? I also do that but it's not working

@bestickley
Copy link

Yep, I'm sure it works for me. I'm not sure why it wouldn't be working for you :(

@kaorun343
Copy link
Owner

@CuongStf
Hi.
Could you ask this question at Vue's official forum?
This issue is not caused by this library.
Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants