我有点酸酸的。
我有一个正在使用的 api:https : //api.myjson.com/bins/1gb9tf
和这里的角度组件:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
results: string[];
// Inject HttpClient into your component or service.
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Making the HTTP Request
this.http
.get('https://api.myjson.com/bins/1gb9tf')
.subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
console.log(data);
})
}
title = 'app';
}
Run Code Online (Sandbox Code Playgroud)
html是:
<!-- Search …Run Code Online (Sandbox Code Playgroud) 假设我制作了一个自定义 JavaScript 文件,即“custom.js”
我将如何将其实现到组件中?
如果我将“custom.js”添加到 index.html,那很好,但是有什么方法可以将它专门添加到组件中?