使用 Angular 和 Tailwind CSS 构建 URL 缩短应用程序
一分耕耘,一分收获!既然打开了这篇文章《使用 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:
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 {
return this.http.post(`${this.apiurl}/shorten`, { originalurl });
}
getallurls(): observable {
return this.http.get(`${this.apiurl}/urls`);
}
getdetails(id: string): observable {
return this.http.get(`${this.apiurl}/details/${id}`);
}
deleteurl(id: string): observable {
return this.http.delete(`${this.apiurl}/delete/${id}`);
}
}
5. 创建缩短 url 组件
生成一个用于缩短 url 的新组件:
ng generate component shorten
更新组件的 html (src/app/shorten/shorten.component.html) 如下所示:
url shortener
@if (errormsg) {} @if (shorturl) {{{ errormsg }}
}shortened url: {{ shorturl }} @if (copymessage) { {{ copymessage }} }
@if (showdeletemodal) {all urls
@if (isloading) {} @else if (error) {loading...} @else { @if (urls.length > 0 && !isloading && !error) {{{ error }}
@for (url of urls; track $index) {
} @else {- }
url: {{ url.shorturl }}} }no urls found.} @if (showdetailsmodal) {confirm deletion
are you sure you want to delete this url?
}url details
@if (isloading) {loading...
} @else {short url: {{ selectedurl.shorturl }}
original url: {{ selectedurl.originalurl }}
clicks: {{ selectedurl.clicks }}
created at: {{ selectedurl.createdat | date: 'medium' }}
expires at: {{ selectedurl.expirationdate | date: 'medium' }}
}
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 = new subject();
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)
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 存储库以详细探索代码。
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
在 PostgreSQL 和 MySQL 中的所有表中查找特定数据
- 上一篇
- 在 PostgreSQL 和 MySQL 中的所有表中查找特定数据
- 下一篇
- 网页设计服务终极指南
-
- 文章 · 前端 | 1天前 | 定时器 · 前端 · 性能排查 · 接口请求 · 轮询 · setInterval · setInterval 页面可见性 clearInterval 前端轮询 请求堆积 定时器清理
- 前端轮询接口越打越多怎么办:从重复定时器到清理机制一步步排查
- 490浏览 收藏
-
- 文章 · 前端 | 1天前 | 前端 · 搜索框 · AbortController · 接口请求 · 状态管理 · Fetch AbortController 前端搜索 请求乱序 旧响应覆盖
- 前端搜索结果倒退怎么办:AbortController 取消旧请求和序号兜底
- 295浏览 收藏
-
- 文章 · 前端 | 1天前 | 前端 · 性能优化 · cls · 懒加载 · Core Web Vitals · 前端 图片懒加载 IntersectionObserver CLS 布局稳定
- 前端图片懒加载布局抖动治理完整流程:占位比例、按需加载和 CLS 复查
- 128浏览 收藏
-
- 文章 · 前端 | 2天前 | 工程化 · 前端 · javascript · css · 弹窗 · 前端 z-index 遮罩层 stacking context Portal 弹窗层级
- 前端弹窗层级治理工作流:从 z-index 混乱到 Portal 容器规范
- 350浏览 收藏
-
- 文章 · 前端 | 2天前 | 前端 · javascript · URL参数 · 列表筛选 · 页面状态 · 前端 筛选条件 列表页 history.replaceState URLSearchParams 刷新还原
- 前端筛选条件刷新后丢失怎么办:从内存状态到 URL 参数一步步排查
- 348浏览 收藏
-
- 文章 · 前端 | 2天前 | 前端 · 性能优化 · 路由 · javascript · 前端 用户体验 滚动位置 路由缓存 scrollRestoration
- 前端详情页返回列表丢失滚动位置怎么办:从复现到恢复一步步排查
- 458浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ljg-skills
- ljg-skills 是李继刚开源的 AI 技能与提示词集合,面向大模型使用者整理了一批可复用的 prompt、角色设定和任务技能模板,适合用于学习提示词设计、搭建个人 AI 工作流和沉淀团队常用智能体能力。
- 438次使用
-
- MELO音乐
- MELO音乐是一站式AI视频与音乐制作助手,对标suno, udio的高品质体验。提供伴奏生成、原创写词、无损导出、哼唱识曲、混音变声等全套音频与短视频编辑工具。无论是流行Kpop、电音说唱、民谣古风、摇滚儿歌还是商用轻音乐,MELO为你免费谱曲,轻松做同款!
- 449次使用
-
- UniScribe
- UniScribe 是一款 AI 音视频转文字与内容整理工具,支持上传音频、视频文件或粘贴 YouTube 链接,自动生成转写文本、摘要、思维导图和关键问题,并支持多格式导出,适合会议记录、课程学习、访谈整理和内容创作复盘。
- 415次使用
-
- 剧云
- 剧云是专业中文剧本创作平台,安全稳定运行十余年,集成AI编剧、剧本医生审核、人物小传、剧情关系图、大纲编写、多人协作、Word导入导出、版权管控功能,数据安全防护,轻松高效创作剧本。
- 587次使用
-
- 万象有声
- 万象有声,一个专为有声创作者打造的新一代智能有声内容创作平台。平台提供专业的智能拆章、智能画本编辑、AI配音、AI生成音效、后期制作、智能对轨、智能审听等有声创作全流程工具,可以帮助创作者高效、低成本创作出引人入胜的有声作品。立即体验,让有声书制作更简单!
- 570次使用
-
- JavaScript函数定义及示例详解
- 2025-05-11 502浏览
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览

