加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2 EXCEPTION没有String的提供者

发布时间:2020-12-17 09:52:46 所属栏目:安全 来源:网络整理
导读:我有一个全新的应用程序创建与ng-cli 用这个非常简单的代码^^ import { Component } from '@angular/core';@Component({ selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']})export class AppComponent { const
我有一个全新的应用程序创建与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) {}
}

而且我已进入控制台了

EXCEPTION:没有String的提供者!

我没有在代码中看到任何错误
怎么了 !

在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;
  }
}

看一眼

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

构造函数中的错误:
export class AppComponent {
  constructor(private my: string) {}
}

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!
  }
}

我建议你从一开始就使用Tutorial,这样你就可以学习Angular的基础知识:)

编辑,你添加的后一部分是一个类,例如为类别文章的类型对象键入对象而不是组件,这是有效的语法:

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

然后,您可以将此类导入AppComponent,并用于分配Article对象.

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

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

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

  constructor() {}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读