使用Axios的contentful.js使用本地主机 [英] contentful.js using Axios is using localhost

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

问题描述

这有点烦人!我正在尝试将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({
        space: environment.space,
        accessToken: environment.cdaAccessToken,
        // pass the adapter to contentful
        adapter: this.axiosAdapter.adapter,
    });
}

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

我在堆栈溢出时发现了另一个问题,他们决定直接使用 HttpClient 来调用API,与此相关的问题是,链接条目显示为单独的对象,这是不好的./p>

当我进入SDK时,发现了一些我可以设置的有趣选项,即hostbasePath,但是设置这些选项却无济于事. 然后我发现了一个proxy配置,该配置期望类型AxiosProxyConfig,它也具有一个host参数. 我设置了所有这些:

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,
    });
}

从此没什么不同. 有人知道如何解决这个问题吗?

解决方案

这是ngx-axios-adapter库的已知问题.解决方法如下: 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);
  }
});
 

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使用本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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