-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to import module to external file #101
Comments
I was just going to ask the same question. Additional question would be then how to use those apis from pages/vuex actions? Basically, what is the best way to refer to axios from plugins or other js files? |
@pi0 @atinux @alexchopin can you guys help with this one? |
@lukasborawski I think the following solution is far more elegant.
then use it like this (remember to register
|
Following discussion [here](nuxt-community#101) I thought it would be good to provide api definer feature from nuxt/axios module itself. This is a common pattern and putting it here perfectly fits in Nuxt way of doing things. UseCase: Many prefer to define their api in a file and use that rather than calling axios directly. This patch makes it very easy. Basically, user would define the api in a file e.g. `api-def.js` ``` export default axios => ({ getOrders(period = "Today") { return axios.get(`api/orders?period=${period}`) }, getUsers() { return axios.get(`/api/users`) } }) ``` then in nuxt.config.js ``` import apiFactory from "./api-def.js" .... axios: { .... api: { factory: apiFactory } }, ``` and that is it. Api woulld be available to plugins, actions, mutators and components ``` -> app.$api or ctx.$api -> this.$api in vue components -> this.$api in store actions/mutations ``` This looks so much better ``` async asyncData({ api }) { let { data } = await $api.getOrders() return { orders: data } }, ```
I also suggested this as an addition to axios module |
I'd recommend this above to be added with some config to avoid complexity |
Great stuff :) |
@husayt I couldn't make your code work, keep saying |
I would need to see your code to say something.
…On Wed, 11 Apr 2018, 08:06 Tim Yao, ***@***.***> wrote:
@husayt <https://github.com/husayt> I couldn't make your code work, keep
saying Cannot read property 'getTest' of undefined. getTest is my version
of your getProducts. Do you know why?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#101 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABHXrBJxEgSWYlK8BzGr_0o9lmOYHZTyks5tnZ1TgaJpZM4SCYtF>
.
|
It's a private repo, I am not able to share with you. But I used exactly same code as you posted. Thanks anyway @husayt |
Hi, I know that this thread is really old now. so yeah I'm wondering if this is still the best and most clean solution for this problem? I'm Referring to the fix that @husayt made -- Husayt's Fix I'm currently using this as my solution last time. it has been 2 years now and I wanted to update my code to be more readable and much easy to document and test. |
@MerrySean it still works for me too. I have seen some other approaches, but this is by far teh most simple one and works like a brick. |
I'm also using the same technique, described a bit more in detail in my blog post |
Thanks @manniL, but isn't it now recommended to use nuxt http module instead of axios one? From what I understand the latter is not going to be supported moving forward. Although, that doesn't change the main point of the article, still is good to know |
just import this in js file |
So, let's say that I'm using axios module and extending it as a plugin. Now i need to import it to the external api.js file to define methods to fetch some data.
plugins/axios.js
api/api.js
and some api file
api/feed.js
How to do this? Got undefined errors ...
cc @pi0
The text was updated successfully, but these errors were encountered: