IT/쇼핑몰

Angula 11 Shopping Mall Basic (3) llama ai

higold 2024. 5. 14. 06:35
반응형

Here's a basic example of how you can create a shopping mall app using Angular 11. We'll be building a simple
e-commerce application where users can browse products, add them to their cart, and checkout.

Step 1: Create a new Angular project

Run the following command in your terminal:

ng new shopping-mall-app --routing

This will create a new Angular project with routing enabled. The --routing flag tells Angular to generate the
necessary files for client-side routing.

Step 2: Set up the project structure

Create the following folders and files:

  • app: This will be the main app component.
  • products: This will contain the list of products.
  • cart: This will manage the user's cart.
  • checkout: This will handle the checkout process.

Step 3: Create the Products Component

In the products folder, create a new file called products.component.ts. Add the following code:

import { Component } from '@angular/core';
import { Product } from './product.model';

@Component({
  selector: 'app-products',
  templateUrl: './products.component.html',
  styleUrls: ['./products.component.css']
})
export class ProductsComponent {
  products: Product[];

  constructor() {
    this.products = [
      { id: 1, name: 'Product 1', price: 19.99 },
      { id: 2, name: 'Product 2', price: 9.99 },
      // Add more products here...
    ];
  }
}

This component will display the list of products.

Step 4: Create the Product Model

In the same products folder, create a new file called product.model.ts. Add the following code:

export class Product {
  id: number;
  name: string;
  price: number;
}

This will define the product model.

Step 5: Create the Cart Component

In the cart folder, create a new file called cart.component.ts. Add the following code:

import { Component } from '@angular/core';
import { Product } from '../products/product.model';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.css']
})
export class CartComponent {
  productsInCart: Product[];

  constructor() {
    this.productsInCart = [];
  }

  addProductToCart(product: Product) {
    this.productsInCart.push(product);
  }
}

This component will manage the user's cart. We'll use it to store the products that the user has added to their cart.

Step 6: Create the Checkout Component

In the checkout folder, create a new file called checkout.component.ts. Add the following code:

import { Component } from '@angular/core';
import { CartService } from '../cart/cart.service';

@Component({
  selector: 'app-checkout',
  templateUrl: './checkout.component.html',
  styleUrls: ['./checkout.component.css']
})
export class CheckoutComponent {
  constructor(private cartService: CartService) {}

  checkout() {
    // Implement the checkout logic here...
  }
}

This component will handle the checkout process. We'll use it to clear the user's cart and redirect them to a confirmation page.

Step 7: Create the Routing Configuration

In the app-routing.module.ts file, add the following code:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    component: ProductsComponent
  },
  {
    path: 'cart',
    component: CartComponent
  },
  {
    path: 'checkout',
    component: CheckoutComponent
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

This will define the routes for our app.

Step 8: Add Routing in the App Component

In the app.component.ts file, add the following code:

import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent {
}

This will enable client-side routing in our app.

That's it! You now have a basic shopping mall app with Angular 11. Of course, this is just the starting point - you'll want to add more features, such as user authentication and payment processing.

I hope this helps! Let me know if you have any questions or need further guidance.

반응형