angular 7 in dashboard.component.ts - angular7

src/app/dashboard/dashboard.component.ts:9:14
9 export class DashboardComponent implements OnInit {
~~~~~~~~~~~~~~~~~~
'DashboardComponent' is declared here.
Error: src/app/dashboard/dashboard.component.ts:11:23 - error NG2003: No suitable injection token for parameter 'studentService' of class 'DashboardComponent'.
Consider using the #Inject decorator to specify an injection token.
11 constructor(private studentService:StudentService) { }
~~~~~~~~~~~~~~
src/app/dashboard/dashboard.component.ts:11:38
11 constructor(private studentService:StudentService) { }
~~~~~~~~~~~~~~
This type does not have a value, so it cannot be used as injection token.
Error: src/app/dashboard/dashboard.component.ts0m:3:32 - error TS2307: Cannot find module '../student.service' or its corresponding type declarations.
3 import { StudentService } from '../student.service';
~~~~~~~~~~~~~~~~~~~~

In the module that this component is imported in, try adding the CommonModule
imports: [CommonModule],
This might be one of the issues.

Related

Get services extended from a certain interface

Is possible to get all services extended from a certain interface in grails
So I can collect them within one service
Yes, I think you can do this by autowiring to List type. Let's say you have three implementations of an interface BaseService in grails-app/services as:
grails-app/services/exampleapp/BaseService.groovy
package exampleapp
interface BaseService {
}
grails-app/services/exampleapp/OneImplService.groovy
package exampleapp
class OneImplService implements BaseService {
}
grails-app/services/exampleapp/TwoImplService.groovy
package exampleapp
class TwoImplService implements BaseService {
}
Now, you can inject all implementations of BaseSerive as follows:
grails-app/services/exampleapp/TestService.groovy
package exampleapp
import org.springframework.beans.factory.annotation.Autowired
class TestService {
#Autowired
List<BaseService> baseServiceList
void doSomething() {
assert baseServiceList.size() == 2
}
}
Please note that the above example is tested with Grails 5 web application. But, I would be surprised if this does not work in the previous versions of Grails.

Angular 5: Can't resolve all parameters for component

I've encountered a problem with injecting services in angular 5 as in tutorial.
I have a simple service
#Injectable()
export class SimpleService {
...
}
And a simple component:
#Component({...})
export class SimpleComponent {
constructor(private simpleService SimpleService) {}
}
A service is set to providers in module:
#NgModule({
...
providers: [SimpleService]
...
})
export class SimpleModule {}
I see the following error in console:
Error: Can't resolve all parameters for SimpleComponent
However, if I inject the SimpleService with #Inject like
#Inject(SimpleService) private simpleService: SimpleService
the error disappears.
What am I doing wrong?
UPDATE:
I saw several answers where people advise to add emitDecoratorMetadata: true to tsconfig file. But this line is already there
It turned out that the reason for this error was an incorrect import of polyfills.ts. Previously I ejected my cli app and removed polyfills entry point from my webpack config. After that I imported them in my main.ts. But I imported polyfills after I imported AppModule. After I moved import './polyfills.ts' statement to the top of the file the error disappeared.
Another option for fixing this error is moving to AOT compilation:
module: {
rules: [
...
{
"test": /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
"use": [
"#ngtools/webpack"
]
}
]
}
plugins: [
...
new AngularCompilerPlugin({
mainPath: 'main.ts',
platform: PLATFORM.Browser,
hostReplacementPaths: {
'environments/environment.ts': 'environments/environment.ts'
},
sourceMap: true,
tsConfigPath: 'src/app/tsconfig.json',
skipCodeGeneration: false // this line is responsible for AOT
})
]
If you use AOT compilation instead JIT you won't need emitDecoratorMetadata in your tsconfig.
I had a similar issue and this may help others, as this was the closest match I found while searching.
I had an error in my constructor function. I forgot to declare the type of the Service:
I had written:
constructor(
private myService
){}
instead of:
constructor(
private myService: MyService
){}

Dependency Injection in Angular 2

I thought DI was implemented to allow use the same services over the application, and change them as needed. However this snippet (Angular 2.0.0-beta.0) refuses to work:
# boot.ts
import {ProjectService} from './project.service'
bootstrap(AppComponent, [ProjectService]);
# my.component.ts
export class MyComponent {
constructor(project: ProjectService) {
}
}
and with explicit service requirement it works:
# my.component.ts
import {ProjectService} from './project.service';
export class MyComponent {
constructor(project: ProjectService) {
}
}
The official doc is somewhat inconsistent, but has the same in the plunkr example:
# boot.ts
import {HeroesListComponent} from './heroes-list.component';
import {HeroesService} from './heroes.service';
bootstrap(HeroesListComponent, [HeroesService])
# heroes-list.component.ts
import {HeroesService} from './heroes.service';
Is this the intended way of DI usage? Why we have to import service in every class requiring it, and where are the benefits if we can't just describe the service once on boot?
This isn't really related to dependency injection. You can't use a class in TS that is not imported.
This line references a class and DI derives from the type what instance to inject.
constructor(project: ProjectService) {
If the type isn't specified by a concrete import, DI can't know which of all possible ProjectService classes should be used.
What you can do for example, is to request a type (ProjectService) and get a different implementation (subclass like MockProjectService or EnhancedProjectService,...)
bootstrap(HeroesListComponent, [provide(ProjectService useClass: MockProjectService)]);
this way DI would inject a MockProjectService for the following constructor
constructor(project: ProjectService) {

CDI will not work with implicit #Dependent scope, unsatisfied injection point compile time error

The container is Glassfish 4.1
I am having a very weird issue with CDI right now. If I don't annotate my NumberGenerator services #Dependent, then I keep getting unsatisfied injection point error when I run the app. But if I explicitly annotate my NumberGenerator implementations, then everything will work. In one word, if I want dependency injection with the #Dependent which is the default scope, I must specify it explicitly.
public interface NumberGenerator {
String generateNumber();
}
The first implementation of NumberGenerator
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
#ThirteenDigits
#Dependent
public class IsbnGenerator implements NumberGenerator {
#Inject
private Logger logger;
#Override
public String generateNumber() {
String isbn = "13-84356-" + Math.abs(new Random().nextInt());
logger.log(Level.INFO, "Generated ISBN : {0}", isbn);
return isbn;
}
}
The second implementation of NumberGenerator
import java.util.Random;
import java.util.logging.Logger;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
#EightDigits
#Dependent
public class IssnGenerator implements NumberGenerator {
#Inject
private Logger logger;
#Override
public String generateNumber() {
String issn = "8-" + Math.abs(new Random().nextInt());
logger.info("Generated ISSN : " + issn);
return issn;
}
}
This is where the NumberGenerator will be injected
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
#Dependent
public class BookService {
#Inject
#EightDigits
private NumberGenerator numberGenerator;
public Book createBook(String title, float price, String description) {
Book book = new Book(title, price, description);
book.setNumber(numberGenerator.generateNumber());
return book;
}
}
Finally, the BookService is injected into this JSF managed bean to create a Book instance.
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
#Named
#Dependent /* if I leave this out, then this bean will not display
the book instance properties on the JSF page, I just see
a blank screen, but when I add this #Dependent annotation
the JSF page displays the dummy content below.
*/
public class MyBean {
#Inject
private BookService bookService;
public Book getBook() {
return bookService.createBook("Dummy Title", 21.05f, "Dummy Description");
}
}
As you can see, I have to use #Dependent for the default scope every time I want DI. Right now, I am injecting the IssnGenerator with the qualifier #EightDigits into the BookService class, and if I remove the #Dependent from the IssnGenerator, I receive this compile error.
Unsatisfied dependencies for type NumberGenerator with qualifiers #EightDigits at injection point [BackedAnnotatedField] #Inject #EightDigits private BookService.numberGenerator
Thank you for any suggestion.
If you do not specify a META-INF/beans.xml file, which seems to be your case, you got an implicit bean archive (see CDI-1.1 §12.1).
In an implicit bean archive, only beans with a bean defining annotation will be discovered by the CDI engine.
Any scope is a bean defining annotation (see CDI-1.1 §2.5). That's why if you add a scope, like #Dependent, you bean gets discovered by CDI and you don't have the unsatisfied dependency error.
The fact that #Dependent is the default scope is not relevant here because without the any scope your beans is simply not discovered.
If you add a META-INF/beans.xml file with the bean-discovery-mode set to all, then you got an explicit bean archive, this means that all beans in your archive will be discovered and will have the #Dependent scope by default.

Angular Dart custom injection failing in dart2js

I have a podo class CarStorage for angular to inject.
class CarStorage {
int _carIdCounter = 0;
...
I wire it in using type:
module
..type(CarForm)
..type(CarItemsList)
..type(CarStorage)
;
It works in dartium but fails to load in dart2js. I get the following error:
Illegal argument(s): No type factory provided for CarStorage! (resolving GarageAppController -> CarStorage)
I discovered through some bug reports and guess work you need to add the DI Injectable annotation:
import "package:angular/angular.dart";
#Injectable()
class CarStorage {
...
edit: Fixed the import.
Do NOT use import 'package:di/annotations.dart'; this causes errors

Resources