5个业务框架的简单使用案例:
- React
React 可以用于构建各种类型的前端应用,包括单页面应用、多页面应用、移动应用等等。以下是一个简单的 React 组件,用于展示一个带有按钮的计数器:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<div>
<p>当前计数为:{count}</p>
<button onClick={handleClick}>增加计数</button>
</div>
);
}
export default Counter;
- Vue
Vue 可以用于开发各种类型的 Web 应用程序,并且提供了许多有用的功能和工具。以下是一个基于 Vue CLI 创建的简单 TodoMVC 应用程序:
<template>
<div class="todos">
<header>
<h1>Todo List</h1>
<form @submit.prevent="addTodo">
<input type="text" v-model="newTodo" placeholder="添加任务" />
</form>
</header>
<ul>
<li v-for="(todo, index) in todos" :key="todo.id">
<input type="checkbox" v-model="todo.completed" />
<span :class="{ completed: todo.completed }">{{todo.title}}</span>
<button @click="removeTodo(index)">删除</button>
</li>
</ul>
</div>
</template>
<script>
export default {
name: "TodoList",
data() {
return {
todos: [
{ id: 1, title: "学习 Vue", completed: false },
{ id: 2, title: "构建 Todo 应用", completed: true },
{ id: 3, title: "学习 Vuex", completed: false },
],
newTodo: "",
};
},
methods: {
addTodo() {
if (this.newTodo.trim() !== "") {
this.todos.push({
id: Date.now(),
title: this.newTodo,
completed: false,
});
this.newTodo = "";
}
},
removeTodo(index) {
this.todos.splice(index, 1);
},
},
};
</script>
- Django
Django 可以用于开发各种类型的 Web 应用程序,包括社交网络、电子商务应用等等。以下是一个简单的 Django 视图,用于展示一个带有表单的登录页面:
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
def login_view(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
message = '无法登录,请检查用户名和密码。'
else:
message = ''
return render(request, 'login.html', {'message': message})
- Ruby on Rails
Ruby on Rails 可以用于开发各种类型的 Web 应用程序,包括博客、论坛、新闻网站等等。以下是一个简单的 Ruby on Rails 控制器,用于展示一个带有文章列表的博客页面:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to posts_path, notice: '文章创建成功!'
else
render :new
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
- Spring
Spring 可以用于构建各种类型的企业应用程序,包括 CRM、ERP、管理系统等等。以下是一个基于 Spring Boot 的简单 RESTful API,用于展示一个带有用户信息的用户管理系统:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("")
public List<User> getAllUsers() {
return userRepository.findAll();
}
@PostMapping("")
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
Optional<User> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
return optionalUser.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
}
}
@PutMapping("/{id}")
public User updateUserById(@PathVariable Long id, @RequestBody User user) {
Optional<User> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
User existingUser = optionalUser.get();
existingUser.setName(user.getName());
existingUser.setEmail(user.getEmail());
return userRepository.save(existingUser);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
}
}
@DeleteMapping("/{id}")
public void deleteUserById(@PathVariable Long id) {
Optional<User> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
userRepository.deleteById(id);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
}
}
}
以上是几个业务框架的简单使用案例,可以帮助您了解每个框架的基本用法和语法。