有人尝试在独立组件中使用InMemoryWebAPI吗?

huangapple go评论61阅读模式
英文:

Anyone try using InMemoryWebAPI with standalone components?

问题

I am using Angular v16 with standalone components. I implemented InMemoryWebAPI as I have in NgModule-based projects, but it doesn't seem to be working. I keep getting 404 not found.

Anyone try this and have success with InMemoryWebAPI with standalone bootstrapping? Anything else different I need to do to get it to work with standalone components?

Here is my app.config.ts

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom(InMemoryWebApiModule.forRoot(AppData, { delay: 1000 })),
    provideHttpClient(),
    provideRouter(routes, withComponentInputBinding())
  ]
};

Here is my AppData

export class AppData implements InMemoryDbService {

  createDb(): { products: Product[] } {
    const products = ProductData.products;
    console.log(products);
    return { products };
  }

}

And here is my service:

private sub = this.#http.get<Product[]>('api/products')

Thanks for any ideas or debugging tips.

EDIT: I have a stackblitz here: https://stackblitz.com/edit/github-crud-signals-djk

The service currently has the http calls commented out because they were generating 404 errors. It instead hard-codes the data.

英文:

I am using Angular v16 with standalone components. I implemented InMemoryWebAPI as I have in NgModule-based projects, but it doesn't seem to be working. I keep getting 404 not found.

Anyone try this and have success with InMemoryWebAPI with standalone bootstrapping? Anything else different I need to do to get it to work with standalone components?

Here is my app.config.ts

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom(InMemoryWebApiModule.forRoot(AppData, { delay: 1000 })),
    provideHttpClient(),
    provideRouter(routes, withComponentInputBinding())
  ]
};

Here is my AppData

export class AppData implements InMemoryDbService {

  createDb(): { products: Product[] } {
    const products = ProductData.products;
    console.log(products);
    return { products };
  }

}

And here is my service:

  private sub = this.#http.get&lt;Product[]&gt;(&#39;api/products&#39;)

Thanks for any ideas or debugging tips.

EDIT: I have a stackblitz here: https://stackblitz.com/edit/github-crud-signals-djk

The service currently has the http calls commented out because they were generating 404 errors. It instead hard-codes the data.

答案1

得分: 3

我通过与Angular团队的协助,成功解决了这个问题。如果你遇到了类似情况,解决方法是调整引导中导入的顺序。它们应该如下所示:

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    importProvidersFrom(InMemoryWebApiModule.forRoot(AppData, { delay: 1000 })),
    provideRouter(routes, withComponentInputBinding())
  ]
};

请注意,在导入中,HttpClient() 应该在 InMemoryWebApiModule 之前。

英文:

With assistance from the Angular team, I was able to resolve the issue. If you run into this, the solution was to adjust the order of the imports in the bootstrapping. They should look like this:

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    importProvidersFrom(InMemoryWebApiModule.forRoot(AppData, { delay: 1000 })),
    provideRouter(routes, withComponentInputBinding())
  ]
};

Notice that the HttpClient() is imported before InMemoryWebApiModule.

huangapple
  • 本文由 发表于 2023年6月8日 05:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定