使用 Axios 的 contentful.js 正在使用 localhost [英] contentful.js using Axios is using localhost

查看:30
本文介绍了使用 Axios 的 contentful.js 正在使用 localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点烦人!我正在尝试将 Contentful SDK 用于 Angular Universal.在此处的文档中:

https://github.com/contentful/contentful.js/blob/master/ADVANCED.md#angular-universal

并根据

https://github.com/patrickhousley/ngx-axios-adapter

我已经安装了 npm install @ngx-axios-adapter/core 然后我调整了我的服务看起来像这样:

constructor(private readonly axiosAdapter: AxiosAngularAdapterService) {this.cdaClient = createClient({空间:environment.space,accessToken: environment.cdaAccessToken,//将适配器传递给 contentful适配器:this.axiosAdapter.adapter,});}

问题在于它不调用 cdn:https://cdn.contentful.com 而是调用我的本地主机:http://localhost:4200 这真的很令人沮丧.我不是唯一遇到这个问题的人,但我看不到任何人解决了这个问题.

我发现了另一个关于堆栈溢出的问题,他们决定直接使用 HttpClient 调用 API,我遇到的问题是链接条目显示为单独的对象,这是不好的.

当我深入研究 SDK 时,我发现了一些可以设置的有趣选项,即 hostbasePath,但设置这些没有任何作用.然后我找到了一个 proxy 配置,它需要 AxiosProxyConfig 类型,它也有一个 host 参数.我设置了所有这些:

constructor(private readonly axiosAdapter: AxiosAngularAdapterService) {this.cdaClient = createClient({空间:environment.space,accessToken: environment.cdaAccessToken,主机:'https://cdn.contentful.com',basePath: '东西',代理: {主机:'https://cdn.contentful.com',},//将适配器传递给 contentful适配器:this.axiosAdapter.adapter,});}

这并没有什么区别.有谁知道如何解决这个问题?

解决方案

这是 ngx-axios-adapter 库的已知问题.这是一个解决方法:https://github.com/patrickhousley/ngx-axios-adapter/issues/13#issuecomment-627326291

<块引用>

this.client = createClient({空间:appConfig.contentful.spaceId,accessToken: appConfig.contentful.token,适配器:(配置)=>{config.url = config.baseURL + '/' + config.url;//修复 Angular 9返回 this.axiosAdapter.adapter(config);}});

This is a bit annoying! I am trying to use Contentful SDK for Angular Universal. In the documents here:

https://github.com/contentful/contentful.js/blob/master/ADVANCED.md#angular-universal

and as per

https://github.com/patrickhousley/ngx-axios-adapter

I have installed npm install @ngx-axios-adapter/core then I adjusted my service to look like this:

constructor(private readonly axiosAdapter: AxiosAngularAdapterService) {
    this.cdaClient = createClient({
        space: environment.space,
        accessToken: environment.cdaAccessToken,
        // pass the adapter to contentful
        adapter: this.axiosAdapter.adapter,
    });
}

The issue is that it doesn't call the cdn: https://cdn.contentful.com instead it calls my localhost: http://localhost:4200 which is really frustrating. I am not the only one that has had this issue, but I can't see anyone that has resolved the problem.

I found another question on stack overflow and they decided to invoke the API directly using HttpClient the problem I have with this, is that linked entries appear as a separate object, which is no good.

When I dived into the SDK I found some interesting options that I could set, namely host and basePath, but setting these did nothing. Then I found a proxy config which expects type AxiosProxyConfig which also has a host paramter. I set all of these:

constructor(private readonly axiosAdapter: AxiosAngularAdapterService) {
    this.cdaClient = createClient({
        space: environment.space,
        accessToken: environment.cdaAccessToken,
        host: 'https://cdn.contentful.com',
        basePath: 'something',
        proxy: {
            host: 'https://cdn.contentful.com',
        },
        // pass the adapter to contentful
        adapter: this.axiosAdapter.adapter,
    });
}

And it made no difference what so ever. Does anyone know how to fix this?

解决方案

This is a known issue with the ngx-axios-adapter library. Here's a workaround: https://github.com/patrickhousley/ngx-axios-adapter/issues/13#issuecomment-627326291

this.client = createClient({
  space: appConfig.contentful.spaceId,
  accessToken: appConfig.contentful.token,
  adapter: (config) => {
    config.url = config.baseURL + '/' + config.url; // fix for Angular 9
    return this.axiosAdapter.adapter(config);
  }
});

这篇关于使用 Axios 的 contentful.js 正在使用 localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆