I implemented this functionality in my angular application and thought that it can be helpful for other developers as well. I have searched it at multiple places and read multiple blogs.
Steps -
install swagger-ui
npm i swagger-ui
Configure the angular.json file and swagger-ui CSS module to it.
"styles": [ "./node_modules/swagger-ui/dist/swagger-ui.css"]
Add the import statement of swagger-UI to your component.ts file.
import SwaggerUI from 'swagger-ui';
Add the below code in your component's ngOnInit.
// example-swagger-exp.component.ts
ngOnInit() {
SwaggerUI({
domNode: document.getElementById('swagger-ui-item'),
url: 'https://petstore.swagger.io/v2/swagger.json'
});
}
last part of this functionality,
Add the below code to the HTML file of your component
// swagger-exp.component.html
<div id="swagger-ui-item"></div>
Note - One last important thing,
How can you add your swagger.json/swagger.yaml object in step 4?
Simply replace the 'url' parameter with 'spec' and add your JSON object.
SwaggerUI({
domNode: document.getElementById('api-data'),
spec : your json object
});