例外:无法解析所有参数

编程入门 行业动态 更新时间:2024-10-17 17:18:07
本文介绍了例外:无法解析所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在Angular 2中构建了一个基本应用程序,但是遇到一个奇怪的问题,即我无法将服务注入我的组件之一.但是,它可以很好地注入我创建的其他三个组件中.

I've built a basic app in Angular 2, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three other components I have created, however.

对于初学者来说,这是服务:

For starters, this is the service:

import { Injectable } from '@angular/core'; @Injectable() export class MobileService { screenWidth: number; screenHeight: number; constructor() { this.screenWidth = window.outerWidth; this.screenHeight = window.outerHeight; window.addEventListener("resize", this.onWindowResize.bind(this) ) } onWindowResize(ev: Event) { var win = (ev.currentTarget as Window); this.screenWidth = win.outerWidth; this.screenHeight = win.outerHeight; } }

以及它拒绝使用的组件:

And the component that it refuses to work with:

import { Component, } from '@angular/core'; import { NgClass } from '@angular/common'; import { ROUTER_DIRECTIVES } from '@angular/router'; import {MobileService} from '../'; @Component({ moduleId: module.id, selector: 'pm-header', templateUrl: 'headerponent.html', styleUrls: ['headerponent.css'], directives: [ROUTER_DIRECTIVES, NgClass], }) export class HeaderComponent { mobileNav: boolean = false; constructor(public ms: MobileService) { console.log(ms); } }

我在浏览器控制台中遇到的错误是:

The error I get in the browser console is this:

例外:无法解析HeaderComponent的所有参数:(?).

EXCEPTION: Can't resolve all parameters for HeaderComponent: (?).

我在bootstrap函数中具有该服务,因此它具有提供程序.而且,我似乎可以毫无问题地将其注入到其他任何组件的构造函数中.

I have the service in the bootstrap function so it has a provider. And I seem to be able to inject it into the constructor of any of my other components without issue.

推荐答案

错误#1:忘记装饰器:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?). export class MyFooService { ... }

错误#2:省略"@"符号:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?). Injectable() export class MyFooService { ... }

错误#3:省略()"符号:

//Uncaught Error: Can't resolve all parameters for TypeDecorator: (?). @Injectable export class MyFooService { ... }

错误#4:小写的"i":

//Uncaught ReferenceError: injectable is not defined @injectable export class MyFooService { ... }

错误#5:您忘记了: 从"@ angular/core"导入{可注射};

//Uncaught ReferenceError: Injectable is not defined @Injectable export class MyFooService { ... }

正确:

@Injectable() export class MyFooService { ... }

更多推荐

例外:无法解析所有参数

本文发布于:2023-11-07 10:47:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!