我一直在玩Angular 2 Quickstart.如何在Angular 2中使用/导入http模块?
我看过Angular 2 Todo的.js,但它没有使用http模块.
我已经在package.json中添加"ngHttp": "angular/http",了dependencies,因为我听说Angular 2有点模块化
所以,我正在通过打字稿在他们的网站上关注角度2指南,并且我陷入了http api集成.我正在尝试创建一个简单的应用程序,可以通过soundcloud api搜索一首歌,但是我很难实现和理解如何开始,而且在线资源以很多不同的方式完成它(我相信快速的角度2语法更改)早些时候).
所以目前我的项目看起来像这样
app
components
home
home.component.ts
...
search
search.component.ts
...
app.ts
...
services
soundcloud.ts
bootstrap.ts
index.html
Run Code Online (Sandbox Code Playgroud)
在示例中没有任何花哨的东西,主要文件将是
app.ts
import {Component, View} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {HomeComponent} from './home/home.component';
import {SearchComponent} from './search/search.component';
@Component({
selector: 'app',
templateUrl: 'app/components/app.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true},
{path: '/search', name: 'Search', component: SearchComponent}
])
export class App { }
Run Code Online (Sandbox Code Playgroud)
bootstrap.ts
import {App} from './components/app';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router'; …Run Code Online (Sandbox Code Playgroud) 我正在开发一个带有angular2和gulp的节点应用程序.我已经编写了一个组件文件login.ts,如下所示:
import {Component, View} from 'angular2/angular2';
import {FormBuilder, formDirectives } from 'angular2/forms';
@Component({
selector: 'login',
injectables: [FormBuilder]
})
@View({
templateUrl: '/scripts/src/components/login/login.html',
directives: [formDirectives]
})
export class login {
}
Run Code Online (Sandbox Code Playgroud)
我的bootstrap.ts文件是:
import {bootstrap} from 'angular2/angular2';
import {login} from './components/login/login';
bootstrap(login);
Run Code Online (Sandbox Code Playgroud)
但是当我编译这些文件时,它会给我以下错误:
client\bootstrap.ts(1,25): error TS2307: Cannot find module 'angular2/angular2'.
client\components\login\login.ts(1,31): error TS2307: Cannot find module 'angular2/angular2
client\components\login\login.ts(2,45): error TS2307: Cannot find module 'angular2/forms'.
Run Code Online (Sandbox Code Playgroud)
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"watch": true,
"removeComments": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": …Run Code Online (Sandbox Code Playgroud) 在Angular2中,你可以在那里有一个文件夹/ data /和一个json文件,你可以在localhost:4200/data/something.json访问它.
Angular4中不再可能这样.
任何想法如何让它工作?
我有服务.在构造函数中:
export class Service {
_data: any
constructor(private http: Http) {
this.http
.get('../assets/my_json_file.json')
.map(x => x.json() )
.subscribe( (data) =>
this._data = data
)
console.log(this._data)
}
Run Code Online (Sandbox Code Playgroud)
该console.log收益undefined虽然它显示的数据,如果console.log被移动到传递给函数subscribe.
我的目标是getStuff()在服务中拥有许多功能,可以通过应用程序调用ngOnInit下拉菜单和内容的值
看到这一点,但没有帮助弄清楚出了什么问题