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

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

阅读 3k
1 个回答

应该在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需要导入包
 }


撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进