Angular2 EXCEPTION No provider for String

妖精的绣舞 提交于 2019-12-01 16:55:36

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.

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 :)

EDIT, the latter part you added is a class e.g 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.

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

and ionic serve again

or angular command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!