angular2 - 在index.html中使用angualr2的组件怎么传值到组件中?

查看:153
本文介绍了angular2 - 在index.html中使用angualr2的组件怎么传值到组件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

angular2组件开发,我把它放到index.html中使用@Input这个传值,在组件里没有获取到,怎么破这个组件的传值问题,还有就是想问下就是index这个首页里是不是放的都是整个页面的组件,而不是小组件拼成的页面

解决方案

应该在app.html/component中进行组件开发。
index.html中:

<body>
<app-root>Loading...</app-root>
</body>

app-root标签与app.component中的selector相对应:

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

把app.html作为父组件,子组件都是在其基础上拼接而成。

根据评论再补充下网络请求的内容:

import {Headers, Http, RequestOptions} from "@angular/http";

 request={
 'id':20,
 'name':'Tony'
};

public requestPost(requestUrl, request) {
   const body: string = JSON.stringify(request);//这句是关键
   const header: any = new Headers({'Content-Type': 'application/json'});
   const options: any = new RequestOptions({headers: header});
   return this.http.post(requestUrl, body, options);//http需要导入包
 }


这篇关于angular2 - 在index.html中使用angualr2的组件怎么传值到组件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆