Whi*_*her 9 typescript ionic3 angular
我有一个全新的应用程序使用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) {}
}
Run Code Online (Sandbox Code Playgroud)
而且我已进入控制台了
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;
}
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/Microsoft/TypeScriptSamples/blob/master/greeter/greeter.ts
AJT*_*T82 16
构造函数中的错误:
export class AppComponent {
constructor(private my: string) {}
}
Run Code Online (Sandbox Code Playgroud)
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!
}
}
Run Code Online (Sandbox Code Playgroud)
我建议你从一开始就开始使用Tutorial,这样你就可以学习Angular的基础知识:)
编辑,你添加的后一部分是一个类,例如用于键入你的对象而不是组件,用于类别文章的类型化对象,这是有效的语法:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此类导入到您的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() {}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13267 次 |
最近记录: |