Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
Basic example
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
Similar to computed from Angular, the function passed to injectQuery will be run in the reactive context. In the example below, the query will be automatically enabled and executed when the filter signal changes to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
Reactive example
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
A function that returns query options.
The Angular injector to use.
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): DefinedCreateQueryResult<TData, TError>
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): DefinedCreateQueryResult<TData, TError>
Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
Basic example
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
Similar to computed from Angular, the function passed to injectQuery will be run in the reactive context. In the example below, the query will be automatically enabled and executed when the filter signal changes to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
Reactive example
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
• TQueryFnData = unknown
• TError = Error
• TData = TQueryFnData
• TQueryKey extends QueryKey = QueryKey
• optionsFn
A function that returns query options.
• injector?: Injector
The Angular injector to use.
DefinedCreateQueryResult<TData, TError>
The query result.
The query result.
A function that returns query options.
The Angular injector to use.
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): CreateQueryResult<TData, TError>
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): CreateQueryResult<TData, TError>
Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
Basic example
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
Similar to computed from Angular, the function passed to injectQuery will be run in the reactive context. In the example below, the query will be automatically enabled and executed when the filter signal changes to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
Reactive example
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
• TQueryFnData = unknown
• TError = Error
• TData = TQueryFnData
• TQueryKey extends QueryKey = QueryKey
• optionsFn
A function that returns query options.
• injector?: Injector
The Angular injector to use.
CreateQueryResult<TData, TError>
The query result.
The query result.
A function that returns query options.
The Angular injector to use.
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): CreateQueryResult<TData, TError>
function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
optionsFn,
injector?,
): CreateQueryResult<TData, TError>
Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
Basic example
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
class ServiceOrComponent {
query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
}))
}
Similar to computed from Angular, the function passed to injectQuery will be run in the reactive context. In the example below, the query will be automatically enabled and executed when the filter signal changes to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
Reactive example
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
class ServiceOrComponent {
filter = signal('')
todosQuery = injectQuery(() => ({
queryKey: ['todos', this.filter()],
queryFn: () => fetchTodos(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}
• TQueryFnData = unknown
• TError = Error
• TData = TQueryFnData
• TQueryKey extends QueryKey = QueryKey
• optionsFn
A function that returns query options.
• injector?: Injector
The Angular injector to use.
CreateQueryResult<TData, TError>
The query result.
The query result.
A function that returns query options.
The Angular injector to use.
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
https://tanstack.com/query/latest/docs/framework/angular/guides/queries