### Angular App Setup with NgRx and Routing
Source: https://context7.com/nrwl/nx-examples/llms.txt
Sets up an Angular application with NgRx for state management and Angular Router for navigation. This includes the main entry point, the App module, and the App component.
```typescript
// Main entry point - apps/products/src/main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
```
```typescript
// App module - apps/products/src/app/app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { StoreModule } from '@ngrx/store';
import { AppComponent } from './app.component';
import '@nx-example/shared/header';
import '@nx-example/shared/styles';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
RouterModule.forRoot([
{
path: '',
pathMatch: 'full',
loadChildren: () => import('@nx-example/products/home-page').then(
(module) => module.ProductsHomePageModule
),
},
{
path: 'product',
loadChildren: () => import('@nx-example/products/product-detail-page').then(
(module) => module.ProductsProductDetailPageModule
),
},
], { initialNavigation: 'enabledBlocking' }),
StoreModule.forRoot({}),
],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class AppModule {}
```
```typescript
// App component - apps/products/src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'nx-example-root',
template:
"\n ",
})
export class AppComponent {}
```
--------------------------------
### React App Setup with Routing
Source: https://context7.com/nrwl/nx-examples/llms.txt
Configures a React application using React Router for navigation and integrates shared components. This snippet includes the main entry point, the App component, and Webpack configuration for the React app.
```typescript
// Main entry point - apps/cart/src/main.tsx
import { StrictMode } from 'react';
import * as ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './app/app';
import '@nx-example/shared/styles';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
);
```
```typescript
// App component - apps/cart/src/app/app.tsx
import { Routes, Route, Link } from 'react-router-dom';
import { CartCartPage } from '@nx-example/cart/cart-page';
import '@nx-example/shared/header';
export function App() {
return (
<>
} />
} />
>
);
}
export default App;
```
```javascript
// Webpack configuration - apps/cart/webpack.config.js
const { composePlugins, withNx } = require('@nx/webpack');
const { withReact } = require('@nx/react');
module.exports = composePlugins(withNx(), withReact(), (config) => {
return config;
});
```
--------------------------------
### Nx Commands - Serve, Build, and Test
Source: https://context7.com/nrwl/nx-examples/llms.txt
Common Nx CLI commands for serving applications, building for production, and running unit tests. This covers both Angular and React applications, as well as shared libraries.
```bash
# Serve applications
nx serve products # Start Angular app on localhost:4200
nx serve cart # Start React app on localhost:4201
# Build for production
nx build products --configuration=production
nx build cart --configuration=production
# Run tests
nx test products # Jest unit tests for Angular app
nx test cart # Jest unit tests for React app
nx test shared-cart-state # Test shared cart state library
```
--------------------------------
### Using TypeScript Path Aliases in Imports
Source: https://context7.com/nrwl/nx-examples/llms.txt
This example demonstrates how to import modules from different libraries within the Nx monorepo using the configured path aliases. It showcases the benefit of cleaner and more organized import statements. No specific dependencies are required beyond the TypeScript configuration.
```typescript
// Usage in any TypeScript file
import { CartCartPage } from '@nx-example/cart/cart-page';
import { Product } from '@nx-example/shared/product/types';
import { getProducts } from '@nx-example/shared/product/state';
import '@nx-example/shared/header';
```
--------------------------------
### Nx Commands - Generate Libraries, Lint, Format, and Deploy
Source: https://context7.com/nrwl/nx-examples/llms.txt
Nx CLI commands for generating new libraries (React and Angular), linting projects, formatting code, and deploying applications. These commands facilitate code generation, maintain code quality, and enable deployment.
```bash
# Generate new library
nx g @nx/react:lib my-lib --directory=libs/shared/my-lib
nx g @nx/angular:lib my-angular-lib --directory=libs/products/my-lib
# Lint and format
nx lint products
nx format:write # Format all files with Prettier
# Deploy
nx deploy products # Deploy products app to Netlify
nx deploy cart # Deploy cart app to Netlify
```
--------------------------------
### Nx Commands - E2E, Affected, and Dependency Graph
Source: https://context7.com/nrwl/nx-examples/llms.txt
Nx CLI commands for running end-to-end tests, executing commands only on affected projects, and visualizing the project dependency graph. These commands help optimize workflows and understand project relationships.
```bash
# E2E testing
nx e2e products-e2e # Cypress E2E tests for products app
nx e2e cart-e2e # Cypress E2E tests for cart app
# Affected commands (only test/build what changed)
nx affected:test # Test only affected projects
nx affected:build # Build only affected projects
nx affected:lint # Lint only affected projects
# Dependency graph
nx dep-graph # View interactive dependency graph
```
--------------------------------
### Framework-Agnostic Product Price Display using Web Components
Source: https://context7.com/nrwl/nx-examples/llms.txt
Implements a reusable product price display component using the Web Components API, making it framework-agnostic. It defines an `nx-example-product-price` custom element that takes a 'value' attribute for the price and formats it. Usage examples are provided for React, Angular, and vanilla JavaScript.
```typescript
// Definition (in @nx-example/shared/product/ui)
export class ProductPriceElement extends HTMLElement {
static observedAttributes = ['value'];
private get displayPrice(): string {
return '$' + (this.value / 100).toFixed(2);
}
get value(): number {
return +this.getAttribute('value');
}
set value(price: number) {
this.setAttribute('value', price.toString());
}
attributeChangedCallback(name: string) {
if (name === 'value') {
this.textContent = this.displayPrice;
}
}
}
customElements.define('nx-example-product-price', ProductPriceElement);
// React usage
import '@nx-example/shared/product/ui';
function ProductCard({ product }) {
return (
);
}
```
--------------------------------
### Cart State Management with NgRx and React Hooks
Source: https://context7.com/nrwl/nx-examples/llms.txt
Manages cart state using a Redux pattern. Supports both Angular with NgRx and React with hooks. It defines cart state, items, and actions for updating quantities. Dependencies include '@nx-example/shared/cart/state' for state logic and '@ngrx/store' for Angular.
```typescript
// Define cart state and actions
import { CartActionTypes, SetQuantity } from '@nx-example/shared/cart/state';
import { cartReducer, initialState } from '@nx-example/shared/cart/state';
// Cart state structure
interface CartState {
items: CartItem[];
}
interface CartItem {
productId: string;
quantity: number;
}
// React usage with useReducer
import { useReducer } from 'react';
function CartComponent() {
const [cartState, dispatch] = useReducer(cartReducer, initialState);
// Update quantity
const handleQuantityChange = (productId: string, quantity: number) => {
dispatch(new SetQuantity(productId, quantity));
};
return (
);
};
```
--------------------------------
### Angular Product List Page: Module and Component
Source: https://context7.com/nrwl/nx-examples/llms.txt
Defines the Angular module and component for the product list page. It utilizes NgRx for state management to fetch and display products. The module is designed for lazy loading, and the component uses Angular's async pipe to handle observable data streams for the product list.
```typescript
// Module definition
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { SharedProductStateModule } from '@nx-example/shared/product/state';
import '@nx-example/shared/product/ui';
@NgModule({
imports: [
CommonModule,
SharedProductStateModule,
RouterModule.forChild([
{ path: '', pathMatch: 'full', component: HomePageComponent },
]),
],
declarations: [HomePageComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class ProductsHomePageModule {}
// Component implementation
import { Component, inject } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { getProductsState, getProducts, ProductsPartialState, Product } from '@nx-example/shared/product/state';
@Component({
selector: 'products-home-page',
template: `
Product Catalog
@for (product of products | async; track product.id) {