Utilizing Ember and Typescript: Guide on Injecting Router Service

I am currently facing an issue while trying to inject RouterService into my controller:

import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import RouterService from '@ember/routing/router-service';


export default class Search extends Controller.extend({
  // anything which *must* be merged to prototype here
}) {
    @service router!: RouterService;


    @action
    actionClick(){
        this.router.transitionTo('protected.apk.detail')
    }
}

// DO NOT DELETE: this is how TypeScript knows how to look up your controllers.
declare module '@ember/controller' {
  interface Registry {
    'search': Search;
  }
}

Unfortunately, I encountered an error message that reads:

Error: Assertion Failed: Attempting to inject an unknown injection: 'service:router'
It seems like the correct syntax should be router:main instead of service:router. Could you please provide guidance on how I can properly inject RouterService? Please note that this controller is part of my engine. Ember version: 3.18.0

Your help is greatly appreciated.

Answer №1

Clues to the type definitions can be found by looking at the bottom of the service.

import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { Registry as Services } from '@ember/service';

export default class Search extends Controller {
    @service router!: Services['router'];

    @action
    actionClick(){
        this.router.transitionTo('protected.apk.detail')
    }
}

Answer №2

Here's a solution:

import Service from '@ember/service';
import type RouterService from '@ember/routing/router-service';

export default class MyService extends Service {
  @service declare router: RouterService;
}

Answer №3

Injecting the router service is done like this:

@service router;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Closing a bootbox alert by clicking on a href link

Utilizing bootbox alert within my Angular2 application to present information to users on the UI. The alert message includes href links, and I want the page to navigate to the clicked link while also closing the bootbox modal. However, I am facing an issue ...

fetching data with Contentful and GatsbyJS

I am currently working on fetching data from Contentful using GraphQL within a Gatsby application, and here is my approach: type AllContentfulBlogs = { allContentfulBlogs: { nodes: Array<{ title?: string | null | undefined, ...

The data type 'number | undefined' cannot be assigned to type 'string' in TypeScript

Need help fixing the number error and converting it to a string? Check out this code snippet: <FlipNumbers height={12} width={12} color="white" background="red" ...

Utilizing the URL path name for data retrieval in Next.js 14 - A step-by-step guide

I'm currently developing a blog using AWS Amplify Gen 2 and GraphQL for a Next.js 14 project with TypeScript. As part of my application, I need to fetch specific data based on the URL path name. Here's how I've approached it: My approach in ...

Why does Angular open the debugger on localhost after a refresh today?

Every time I refresh my angular app today, the debugger (PAUSED ON DEBUGGER) keeps opening. Why is this happening? The debugger is highlighting these lines of code (which are not mine - from core.js): /** * Instantiate all the directives that were previo ...

Configuring Typescript target and library to utilize Promise.allSettled on outdated web browsers

I am currently using TypeScript version 4.3.5 and Node.js version 14.18.1 in my project. The code I am working on is compiled to target both old and new browsers by setting target=es5 in the tsconfig file. I make use of both Promise.all and Promise.allSett ...

Generating Pulumi Outputs for exporting as an external configuration file

I am currently utilizing Cloudrun in GCP and am interested in summarizing the created APIs with API Gateway. To achieve this, a Swagger/OpenAPI v2 document needs to be generated containing the google-generated URLs for the Cloudrun Services. How can I ext ...

unable to form the directory named tns_modules

Facing an issue while trying to run the demo application in Nativescript. Performed the following command: tns run ios Encountered an error: Unable to apply changes on device: DEVICE_ID. Error is: Processing node_modules failed. Error: cp: cannot c ...

The Action-Reducer Mapping feature is encountering a type error when handling multiple types of actions

Earlier today, I posed a question about creating a mapping between redux action types and reducers to handle each type explicitly. After receiving helpful guidance on how to create the mapping, I encountered an error when attempting to use it in creating ...

What is the best way to configure eslint or implement tslint and prettier for typescript?

In my React/Redux project, I recently started integrating TypeScript into my workflow. The eslint configuration for the project is set up to extend the airbnb eslint configurations. Here's a snippet of my current eslint setup: module.exports = { // ...

How can a parent's method be activated only after receiving an event emitter from the child and ensuring that the parent's ngIf condition is met?

Every time the element in the child template is clicked, it triggers the method activateService(name) and emits an event with the name of the selected service using the event emitter serviceSelected. The parent component's method scrollDown(name) is t ...

Configuring Typescript for src and test directories

My TypeScript projects have a structure where the source code is separated from the tests into different directories (src and test). When packaging the final product, I only want to include the source code without the tests because the runtime doesn't ...

Using TypeScript with Vue and Pinia to access the store

Is it necessary to type the Store when importing it to TypeScript in a Pinia Vue project? // Component <p>{{ storeForm.firstName }}</p> // receiving an error "Property 'storeForm' does not exist on type" // Store ...

Navigating a SwipeableDrawer in React with scrolling functionality

I'm currently using a swipeable drawer in React from MUI to display lengthy content. My goal is to keep the title visible even when the drawer is closed, and I was able to achieve this through the following method: MUI SwipeableDrawer I've provi ...

What could be causing the "buffer is not a function" error to occur?

As a beginner with observables, I'm currently working on creating an observable clickstream to avoid capturing the 2 click events that happen during a double-click. However, I keep encountering this error message:- Error: Unhandled Promise rejection ...

How to programmatically generate buttons with click functionality in Angular 2 and above

Is there a way to dynamically create a button in Angular using innerHtml? I have managed to create the button, but unfortunately, its click event is not functioning properly. Can anyone provide guidance on how to resolve this issue? Below is my HTML code ...

Issue encountered: Jest-dom is throwing a TypeError because $toString is not recognized as a function on a project using Typescript, React

I have been facing a challenge while setting up jest and @testing-library/jest-dom for my typescript/react/next.js website. Each time I try running the tests, an error occurs, and I am struggling to identify the root cause. This issue has been perplexing ...

Add information to an array by simply modifying the existing data that shares the same key/value pair

Currently, I am working on the front-end of a delivery web application. On one of the screens, I have implemented a Google map that allows the company owner to track their delivery riders in real-time. The process of implementing the map itself was quite s ...

Preserving reactivity while swapping an item in Vue 3 using the composition API

I am looking to update my object while maintaining reactivity. The variable selectedTime is a Date object, and the function substractMonths will return a new Date object. <span class="calendar__pickable__chevron" @click="selectedTim ...

ng2-select is experiencing difficulties when attempting to retrieve and process data from an API while also casting it within the initData

Issue with ng2-select and API Data Retrieval I am encountering a problem while using ng2-select in my form. I am fetching data from an API and setting it into an array, but it is not functioning correctly. Below is the code snippet I am working with. When ...