简体   繁体   English

Angular2 EXCEPTION没有String的提供者

[英]Angular2 EXCEPTION No provider for String

I've got a brand new app create with a ng-cli with this very simple code ^^ 我有一个全新的应用程序使用ng-cli创建这个非常简单的代码^^

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private my: string) {}
} 

and I've got in the console 而且我已进入控制台了

EXCEPTION: No provider for String! EXCEPTION:没有String的提供者!

I don't see any error in the code so what's wrong ! 我没有在代码中看到任何错误,所以有什么不对!

In ng-book I can read 在ng-book中,我可以阅读

export class Article { 
  title: string; 
  link: string; 
  votes: number;
  constructor(title: string, link: string, votes?: number) { 
    this.title = title;
    this.link = link;
    this.votes = votes || 0;
  }
}

Take a look at 看一眼

https://github.com/Microsoft/TypeScriptSamples/blob/master/greeter/greeter.ts https://github.com/Microsoft/TypeScriptSamples/blob/master/greeter/greeter.ts

Error in constructor: 构造函数中的错误:

export class AppComponent {
  constructor(private my: string) {}
} 

private my: string should not be injected in the constructor, but outside, here assuming it's a variable you want to use in your component. private my: string不应该在构造函数中注入,但在外面,这里假设它是你想在组件中使用的变量。

export class AppComponent {
  private my: string;
  constructor() {
    this.my = 'Hello!'; // if you want to assign a value (in the constructor) to your string, do it here!
  }
} 

I suggest you start of with the Tutorial from the beginning, so you learn the basics of Angular :) 我建议你从一开始就开始使用Tutorial ,这样你就可以学习Angular的基础知识:)

EDIT, the latter part you added is a class eg for typing your object, not a component, for a typed Object of class Article, this is valid syntax: 编辑,你添加的后一部分是一个类,例如用于键入你的对象而不是组件,用于类别文章的类型化对象,这是有效的语法:

export class Article { 
  title: string; 
  link: string; 
  votes: number;
  constructor(title: string, link: string, votes?: number) { 
    this.title = title;
    this.link = link;
    this.votes = votes || 0;
  }
}

Then you can import this class to your AppComponent , and use to assign an Article object. 然后,您可以将此类导入AppComponent ,并用于分配Article对象。

import { Component } from '@angular/core';
import { Article } from './your.path'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  article: Article = new Article('titleHere', 'linkHere', 3)

  constructor() {}
}

I had this error and can solve it 我有这个错误,可以解决它

just need to re-run the build process 只需要重新运行构建过程

by stopping it with cntrl+c 通过cntrl + c停止它

and ionic serve again ionic serve再次ionic serve

or angular command 或角度命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2异常:没有服务提供者 - Angular2 Exception: No provider for Service Angular2中出现异常例外:PlatformLocation是否没有提供程序? - Getting Exception in Angular2 EXCEPTION: No provider for PlatformLocation? Angular2:例外:没有 Angulartics2GoogleAnalytics 的提供者 - Angular2 : EXCEPTION: No provider for Angulartics2GoogleAnalytics Angular2 EXCEPTION:没有e的提供者! (e - > e) - Angular2 EXCEPTION: No provider for e! (e -> e) Angular2:内联模板:8:0由以下原因引起:没有String的提供程序 - Angular2: inline template:8:0 caused by: No provider for String Angular2:创建表单时出现“无t提供者!”的例外情况 - Angular2: Exception “No provider for t!” when creating a form Angular2-browser_adapter.ts原始例外:没有位置信息提供者 - Angular2 - browser_adapter.ts ORIGINAL EXCEPTION: No provider for Location Angular2 beta获得异常“没有路由器提供商! (RouterLink - >路由器)“ - Angular2 beta getting exception “No provider for Router! (RouterLink -> Router)” Angular2:没有String的提供者! [child - > String]在[父级中的数组]中 - Angular2 : No provider for String! (child -> String) in [Array in parent Angular2没有为CompileMetadataResolver提供程序 - Angular2 No provider for CompileMetadataResolver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM