Angular Template Hierarchy and Data Fetching
In this example we show how to implement the WordPress Template Hierarchy in Angular for use with a Headless WordPress backend using WPGraphQL.
Getting Started
Section titled “Getting Started”- Run
npm run example:setupto install dependencies and configure the local WP server. - Configure CORS: Create a file named
.envinside the/backenddirectory and add the following content to set the allowed frontend URL:(For more details on the backend service, please refer to theFRONTEND_URL=http://localhost:4200/backend/README.mdfile.) - Run
npm run backend:startstarts the backend server for template fetching athttp://localhost:3000/api/templates - Run
npm run example:startto start the WordPress server and Angular development server.
Trouble Shooting
Section titled “Trouble Shooting”- I get “Page Not Found. Sorry, the page you are looking for does not exist. Please check the URL.” when opening the Angular app and trying to navigate through it.
- Run
npm run backend:startand verify thathttp://localhost:3000/api/templatesreturns correct data. - Verify that you have created the
/backend/.envfile with the correctFRONTEND_URLto resolve CORS issues. - Check for any errors in the console.
- In some cases, you might have to install
@angular/cliglobally. In/example-app/runnpm install -g @angular/cli@latest - To reset the WP server and re-run setup you can run
npm run example:pruneand confirm “Yes” at any prompts.