GithubHelp home page GithubHelp logo

Comments (5)

hipertracker avatar hipertracker commented on May 4, 2024 1

The current Vue context is not available in setup(), it's available in onMounted().

import axios, { AxiosInstance } from 'axios'

export default async ({ Vue }) => {
  Vue.prototype.$axios = axios
}

declare module 'vue/types/vue' {
  interface Vue {
    $axios: AxiosInstance;
  }
  interface VueConstructor {
    $axios: AxiosInstance;
  }
}

Component:

import {  onMounted, createComponent } from 'vue-function-api'

export default createComponent({
  setup (props, context) {
    let axios
    //...
    onMounted(function (this: Vue) {
      axios = this.$axios
    })
  }
})

Alternative:

import axios from 'axios';
import VueAxios from 'vue-axios';
import { VueConstructor } from 'vue';

export default async ({ Vue }: { Vue: VueConstructor }) => {
  Vue.use(VueAxios, axios);
};

declare module '*.vue' {
  export default Vue
}
import {  onMounted, createComponent } from 'vue-function-api'

export default createComponent({
  setup (props, context) {
    let axios
    //...
    onMounted(function (this: Vue) {
      axios = this.axios
    })
  }
})

from composition-api.

liximomo avatar liximomo commented on May 4, 2024 1
import Vue from 'vue';
import axios  from 'axios'

Vue.prototype.$axios = axios

export default createComponent({
  setup (props, context) {
     let axios = context.root.$axios;
     // ...
  }
})

from composition-api.

liximomo avatar liximomo commented on May 4, 2024

Are you sure you have extended the Vue.prototype?

from composition-api.

IlCallo avatar IlCallo commented on May 4, 2024

Collateral question: is context.root the element root or, as per previous API, the component tree root?

Until now we "littered" Vue prototype to easily access some globals ($axios, $vuex, etc), but this feels wrong with new function API, not being able to get $el from context and not being able to add those shortcuts to the context object itself.

Does it mean we should abandon Vue augmentation and only use VueConstructor augmentation, with stuff like Vue.axios.get() and similar?

from composition-api.

liximomo avatar liximomo commented on May 4, 2024

It should work if you properly extends the Vue.prototype.

from composition-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.