讲师中心 微信公众号
AI工具推荐 视频效率加速

使用 Angular 和 Tailwind CSS 构建 URL 缩短应用程序

大晨吖_2195

大晨吖_2195

发布时间:2024-07-26 19:46:19

|

654人浏览过

|

来源于dev.to

转载

使用 angular 和 tailwind css 构建 url 缩短应用程序

在本博客中,我们将引导您完成使用 angular 作为前端并使用 tailwind css 进行样式创建 url 缩短器应用程序的过程。 url 缩短器是一个方便的工具,可以将长 url 转换为更短、更易于管理的链接。该项目将帮助您了解如何使用现代 web 开发技术构建功能齐全且美观的 web 应用程序。

先决条件

要学习本教程,您应该对 angular 有基本的了解,并对 tailwind css 有一定的了解。确保您的计算机上安装了 node.js 和 angular cli。

项目设置

1. 创建一个新的 angular 项目

首先,通过在终端中运行以下命令来创建一个新的 angular 项目:

ng new url-shortener-app
cd url-shortener-app

2. 安装 tailwind css

接下来,在您的 angular 项目中设置 tailwind css。通过 npm 安装 tailwind css 及其依赖项:

npm install -d tailwindcss postcss autoprefixer
npx tailwindcss init

通过更新 tailwind.config.js 文件来配置 tailwind css:

module.exports = {
  content: [
    "./src/**/*.{html,ts}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

将 tailwind 指令添加到您的 src/styles.scss 文件中:

@tailwind base;
@tailwind components;
@tailwind utilities;

构建 url 缩短器

3. 创建 url 模型

创建 url 模型来定义 url 数据的结构。添加新文件 src/app/models/url.model.ts:

export type urls = url[];

export interface url {
  _id: string;
  originalurl: string;
  shorturl: string;
  clicks: number;
  expirationdate: string;
  createdat: string;
  __v: number;
}

4. 设置 url 服务

创建一个服务来处理与 url 缩短相关的 api 调用。添加新文件 src/app/services/url.service.ts:

digital-solution-designer
digital-solution-designer

系统化设计数字化解决方案,涵盖方案类型识别、政策背景分析、需求分析、建设思路设计、架构设计(业务、功能、数据、技术四维度)、技术选型、具体建设内容、实施规划、风险评估和投资估算的全流程能力。适用于规划类、申报类、可研类、投标类和工作汇报类等多种方案产出场景,覆盖政府数字化转型和企业数字化转型两大核心领域。

下载
import { httpclient } from '@angular/common/http';
import { injectable } from '@angular/core';
import { observable } from 'rxjs';
import { url, urls } from '../models/url.model';
import { environment } from '../../environments/environment';

@injectable({
  providedin: 'root',
})
export class urlservice {
  private apiurl = environment.apiurl;

  constructor(private http: httpclient) {}

  shortenurl(originalurl: string): observable<url> {
    return this.http.post<url>(`${this.apiurl}/shorten`, { originalurl });
  }

  getallurls(): observable<urls> {
    return this.http.get<urls>(`${this.apiurl}/urls`);
  }

  getdetails(id: string): observable<url> {
    return this.http.get<url>(`${this.apiurl}/details/${id}`);
  }

  deleteurl(id: string): observable<url> {
    return this.http.delete<url>(`${this.apiurl}/delete/${id}`);
  }
}

5. 创建缩短 url 组件

生成一个用于缩短 url 的新组件:

ng generate component shorten

更新组件的 html (src/app/shorten/shorten.component.html) 如下所示:

<div class="max-w-md mx-auto p-4 shadow-lg rounded-lg mt-4">
    <h2 class="text-2xl font-bold mb-2">url shortener</h2>
    <form [formgroup]="urlform" (ngsubmit)="shortenurl()">
        <div class="flex items-center mb-2">
            <input class="flex-1 p-2 border border-gray-300 rounded mr-4" formcontrolname="originalurl"
                placeholder="enter your url" required />
            <button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" type="submit">
                shorten
            </button>
        </div>
        @if (urlform.get('originalurl')?.invalid && (urlform.get('originalurl')?.dirty ||
        urlform.get('originalurl')?.touched)) {
        <div class="text-red-500" role="alert" aria-live="assertive">
            @if (urlform.get('originalurl')?.errors?.['required']) {
            url is required.
            }
            @if (urlform.get('originalurl')?.errors?.['pattern']) {
            invalid url format. please enter a valid url starting with http:// or https://.
            }
        </div>
        }
    </form>
    @if (errormsg) {
    <div class="p-4 bg-red-100 rounded mt-4">
        <p class="text-red-500">{{ errormsg }}</p>
    </div>
    }
    @if (shorturl) {
    <div class="p-4 bg-green-100 rounded">
        <p>shortened url: <a class="text-blue-500 hover:text-blue-600" [href]="redirecturl + shorturl"
                target="_blank">{{ shorturl }}</a>
            <button class="ml-2 px-2 py-1 bg-gray-200 text-gray-800 border border-slate-950 rounded hover:bg-gray-300"
                (click)="copyurl(redirecturl + shorturl)">copy</button>
            @if (copymessage) {
            <span class="text-green ml-2">{{ copymessage }}</span>
            }
        </p>
    </div>
    }
</div>

<div class="max-w-md mx-auto mt-4 p-2">
    <h2 class="text-2xl font-bold mb-4">all urls</h2>
    @if (isloading) {
    <div class="max-w-md mx-auto p-4 shadow-lg rounded-lg">
        <div class="text-center p-4">
            loading...
        </div>
    </div>
    }
    @else if (error) {
    <div class="max-w-md mx-auto p-4 shadow-lg rounded-lg">
        <div class="text-center p-4">
            <p class="text-red-500">{{ error }}</p>
        </div>
    </div>
    }
    @else {
    @if (urls.length > 0 && !isloading && !error) {
    <ul>
        @for (url of urls; track $index) {
        <li class="p-2 border border-gray-300 rounded mb-2">
            <div class="flex justify-between items-center">
                <div>
                    url:
                    <a class="text-blue-500 hover:text-blue-600" [href]="redirecturl + url.shorturl" target="_blank">{{
                        url.shorturl }}</a>
                </div>
                <div class="flex justify-between items-center">
                    <button class="px-2 py-1 bg-blue-200 text-blue-800 rounded hover:bg-blue-300"
                        (click)="showdetails(url.shorturl)">details</button>
                    <button class="ml-2 px-2 py-1 bg-gray-200 text-gray-800 rounded hover:bg-gray-300"
                        (click)="copylisturl(redirecturl + url.shorturl, $index)">{{
                        copyindex === $index ? 'copied' : 'copy'
                        }}</button>
                    <button class="ml-2 px-2 py-1 bg-red-200 text-red-800 rounded hover:bg-red-300"
                        (click)="preparedelete(url.shorturl)">delete</button>
                </div>
            </div>
        </li>
        }
    </ul>
    }
    @else {
    <div class="max-w-md mx-auto p-4 shadow-lg rounded-lg">
        <div class="text-center p-4">
            no urls found.
        </div>
    </div>
    }
    }
</div>

@if (showdeletemodal) {
<div class="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50">
    <div class="bg-white p-4 rounded shadow-lg">
        <h3 class="text-xl font-bold mb-2">confirm deletion</h3>
        <p class="mb-4">are you sure you want to delete this url?</p>
        <button class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600" (click)="confirmdelete()">yes,
            delete</button>
        <button class="px-4 py-2 bg-gray-300 text-gray-800 rounded hover:bg-gray-400 ml-2"
            (click)="showdeletemodal = false">cancel</button>
    </div>
</div>
}

@if (showdetailsmodal) {
<div class="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50">
    <div class="bg-white p-4 rounded shadow-lg">
        <h3 class="text-xl font-bold mb-2">url details</h3>
        @if (isloading) {
        <p class="mb-4">loading...</p>
        }
        @else {
        <p class="mb-4">short url: <a class="text-blue-500 hover:text-blue-600"
                [href]="redirecturl + selectedurl.shorturl" target="_blank">{{ selectedurl.shorturl }}</a></p>
        <p class="mb-4">original url: <a class="text-blue-500 hover:text-blue-600" [href]="selectedurl.originalurl"
                target="_blank">{{ selectedurl.originalurl }}</a></p>
        <p class="mb-4">clicks: <span class="text-green-500">{{ selectedurl.clicks }}</span></p>
        <p class="mb-4">created at: {{ selectedurl.createdat | date: 'medium' }}</p>
        <p class="mb-4">expires at: {{ selectedurl.expirationdate | date: 'medium' }}</p>
        <button class="px-4 py-2 bg-gray-300 text-gray-800 rounded hover:bg-gray-400"
            (click)="showdetailsmodal = false">close</button>
        }
    </div>
</div>
}

6. 向组件添加逻辑

更新组件的 typescript 文件(src/app/shorten/shorten.component.ts)以处理表单提交和 api 交互:

import { component, inject, oninit } from '@angular/core';
import { urlservice } from '../services/url.service';
import {
  formcontrol,
  formgroup,
  reactiveformsmodule,
  validators,
} from '@angular/forms';
import { url } from '../models/url.model';
import { environment } from '../../environments/environment';
import { datepipe } from '@angular/common';
import { subject, takeuntil } from 'rxjs';

@component({
  selector: 'app-shorten',
  standalone: true,
  imports: [datepipe, reactiveformsmodule],
  templateurl: './shorten.component.html',
  styleurl: './shorten.component.scss',
})
export class shortencomponent implements oninit {
  shorturl: string = '';
  redirecturl = environment.apiurl + '/';
  copymessage: string = '';
  copylistmessage: string = '';
  urls: url[] = [];
  showdeletemodal = false;
  showdetailsmodal = false;
  urltodelete = '';
  copyindex: number = -1;
  selectedurl: url = {} as url;
  isloading = false;
  isloading = false;
  error: string = '';
  errormsg: string = '';
  urlform: formgroup = new formgroup({});
  private unsubscribe$: subject<void> = new subject<void>();

  urlservice = inject(urlservice);

  ngoninit() {
    this.urlform = new formgroup({
      originalurl: new formcontrol('', [
        validators.required,
        validators.pattern('^(http|https)://.*$'),
      ]),
    });
    this.getallurls();
  }

  shortenurl() {
    if (this.urlform.valid) {
      this.urlservice.shortenurl(this.urlform.value.originalurl).pipe(takeuntil(this.unsubscribe$)).subscribe({
        next: (response) => {
          // console.log('shortened url: ', response);
          this.shorturl = response.shorturl;
          this.getallurls();
        },
        error: (error) => {
          console.error('error shortening url: ', error);
          this.errormsg = error?.error?.message || 'an error occurred!';
        },
      });
    }
  }

  getallurls() {
    this.isloading = true;
    this.urlservice.getallurls().pipe(takeuntil(this.unsubscribe$)).subscribe({
      next: (response) => {
        // console.log('all urls: ', response);
        this.urls = response;
        this.isloading = false;
      },
      error: (error) => {
        console.error('error getting all urls: ', error);
        this.isloading = false;
        this.error = error?.error?.message || 'an error occurred!';
      },
    });
  }

  showdetails(id: string) {
    this.showdetailsmodal = true;
    this.getdetails(id);
  }

  getdetails(id: string) {
    this.isloading = true;
    this.urlservice.getdetails(id).subscribe({
      next: (response) => {
        // console.log('url details: ', response);
        this.selectedurl = response;
        this.isloading = false;
      },
      error: (error) => {
        console.error('error getting url details: ', error);
        this.error = error?.error?.message || 'an error occurred!';
      },
    });
  }

  copyurl(url: string) {
    navigator.clipboard
      .writetext(url)
      .then(() => {
        // optional: display a message or perform an action after successful copy
        console.log('url copied to clipboard!');
        this.copymessage = 'copied!';
        settimeout(() => {
          this.copymessage = '';
        }, 2000);
      })
      .catch((err) => {
        console.error('failed to copy url: ', err);
        this.copymessage = 'failed to copy url';
      });
  }

  copylisturl(url: string, index: number) {
    navigator.clipboard
      .writetext(url)
      .then(() => {
        // optional: display a message or perform an action after successful copy
        console.log('url copied to clipboard!');
        this.copylistmessage = 'copied!';
        this.copyindex = index;
        settimeout(() => {
          this.copylistmessage = '';
          this.copyindex = -1;
        }, 2000);
      })
      .catch((err) => {
        console.error('failed to copy url: ', err);
        this.copylistmessage = 'failed to copy url';
      });
  }

  preparedelete(url: string) {
    this.urltodelete = url;
    this.showdeletemodal = true;
  }

  confirmdelete() {
    // close the modal
    this.showdeletemodal = false;
    // delete the url
    this.deleteurl(this.urltodelete);
  }

  deleteurl(id: string) {
    this.urlservice.deleteurl(id).subscribe({
      next: (response) => {
        // console.log('deleted url: ', response);
        this.getallurls();
      },
      error: (error) => {
        console.error('error deleting url: ', error);
        this.error = error?.error?.message || 'an error occurred!';
      },
    });
  }

  ngondestroy() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}

7.更新应用程序组件的html文件(src/app/app.component.html)

<router-outlet></router-outlet>

8.更新应用程序配置文件(src/app/app.config.ts)

import { applicationconfig, providezonechangedetection } from '@angular/core';
import { providerouter } from '@angular/router';

import { routes } from './app.routes';
import { providehttpclient } from '@angular/common/http';

export const appconfig: applicationconfig = {
  providers: [
    providezonechangedetection({ eventcoalescing: true }),
    providerouter(routes),
    providehttpclient(),
  ],
};

9.更新应用程序路由文件(src/app/app.routes.ts)

import { Routes } from '@angular/router';

export const routes: Routes = [
  {
    path: '',
    loadComponent: () =>
      import('./shorten/shorten.component').then((m) => m.ShortenComponent),
  },
];

结论

您已经使用 angular 和 tailwind css 成功构建了 url 缩短器应用程序。该项目演示了如何集成现代前端技术来创建功能强大且时尚的 web 应用程序。借助 angular 的强大功能和 tailwind css 实用程序优先的方法,您可以轻松构建响应灵敏且高效的 web 应用程序。

请随意通过添加用户身份验证等功能来扩展此应用程序。祝您编码愉快!

立即学习“前端免费学习笔记(深入)”;

探索代码

访问 github 存储库以详细探索代码。


热门AI工具

更多
花生AI
花生AI Hot

一款AI视频创作工具,主要用于B站推出的AI视频创作工具,适合需要提升相关任务效率的用户。

Seko
Seko Hot

一款AI视频创作工具,主要用于商汤科技推出的创编一体的AI短视频创作Agent,适合需要提升相关任务效率的用户。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

UpDream
UpDream Hot

一款AI视频创作工具,主要用于哔哩哔哩推出的自研AI视频创作工具,适合需要提升相关任务效率的用户。

音述AI
音述AI Hot

一款AI音频处理工具,主要用于音述AI是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

创客贴AI

创客贴AI是一款AI图像与设计工具,AI辅助的智能在线设计工具。

火山引擎

火山引擎是一款面向企业的云计算与AI服务平台。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

相关专题

更多
TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

312

2026.02.13

TypeScript全栈项目架构与接口规范设计
TypeScript全栈项目架构与接口规范设计

本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。

520

2026.02.25

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

391

2026.03.13

TypeScript 全栈开发进阶指南
TypeScript 全栈开发进阶指南

面向有 JavaScript 基础的开发者,深入讲解 TypeScript 的类型系统与全栈开发实践。

306

2026.06.03

TypeScript Node.js 全栈工程化与Monorepo架构实践
TypeScript Node.js 全栈工程化与Monorepo架构实践

本专题围绕 TypeScript 在 Node.js 全栈开发中的工程化实践展开,系统讲解 Monorepo 架构设计、包管理策略、模块复用机制以及服务端与前端统一类型系统的构建方法。通过真实项目案例,帮助开发者提升大型全栈项目的可维护性与协作效率。

678

2026.06.16

js正则表达式
js正则表达式

php中文网为大家提供各种js正则表达式语法大全以及各种js正则表达式使用的方法,还有更多js正则表达式的相关文章、相关下载、相关课程,供大家免费下载体验。

4616

2023.06.20

js获取当前时间
js获取当前时间

JS全称JavaScript,是一种具有函数优先的轻量级,解释型或即时编译型的编程语言;它是一种属于网络的高级脚本语言,主要用于Web,常用来为网页添加各式各样的动态功能。js怎么获取当前时间呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

1395

2023.07.28

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

1818

2023.08.03

Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

60

2026.10.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 1.6万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 6.1万人学习

CSS教程
CSS教程

共754课时 | 99.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn