Deploying an Angular application to GitHub Pages involves building the application for production and then pushing the generated files to a specific branch in your GitHub repository.
Steps to Deploy an Angular App to GitHub Pages:
Create a GitHub Repository: If you don't already have one, create a new public repository on GitHub for your Angular project.
Build Your Angular App for Production:
Use the Angular CLI to build a production-ready version of your application. The --base-href flag is crucial for ensuring the application's assets are correctly referenced on GitHub Pages.
ng build --configuration production --base-href "https://<your-username>.github.io/<your-repo-name>/"
Replace <your-username> with your GitHub username.
Replace <your-repo-name> with the name of your GitHub repository.
If you are deploying to a user or organization page (e.g., username.github.io), you can omit the repository name from the base-href.
Install angular-cli-ghpages (Optional but Recommended):
This tool simplifies the deployment process by handling the creation and pushing of the gh-pages branch.
npm install -g angular-cli-ghpages
Deploy to GitHub Pages.
If you installed angular-cli-ghpages:
npx angular-cli-ghpages --dir=dist/<your-project-name>
Replace <your-project-name> with the name of your Angular project (found in your angular.json file under defaultProject).
This command will build your app (if not already built) and push the contents of the specified dist directory to the gh-pages branch of your repository.
Comments
Post a Comment