英文:
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<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.
答案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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论