第一部分:框架概述
1.1 React框架
React是由Facebook开发的一个用于构建用户界面的JavaScript库。它采用组件化开发模式,使得开发者可以构建灵活且可复用的UI组件。React的核心思想是虚拟DOM(Virtual DOM),它通过比较虚拟DOM和实际DOM的差异来高效地更新DOM。
1.2 Vue框架
Vue是一个渐进式JavaScript框架,易于上手,同时也能进行大型项目的开发。Vue的核心特性包括响应式数据绑定、组件系统、虚拟DOM等。Vue的设计理念强调易用性、灵活性和渐进式。
1.3 Angular框架
Angular是由Google开发的,基于TypeScript的Web开发框架。它提供了一个完整的解决方案,包括模型-视图-控制器(MVC)模式、依赖注入、表单处理等。Angular旨在构建高性能的、可维护的前端应用。
第二部分:入门篇
2.1 React入门
2.1.1 环境搭建
- 安装Node.js和npm(Node.js包管理器)。
- 使用create-react-app创建项目。
npx create-react-app my-app
2.1.2 JSX语法
React使用JSX来描述UI结构,JSX是一种JavaScript的语法扩展。
import React from 'react';
function App() {
return (
<div>
<h1>Hello, world!</h1>
</div>
);
}
export default App;
2.1.3 组件化开发
React采用组件化开发模式,将UI拆分为多个可复用的组件。
import React from 'react';
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Alice" />
<Welcome name="Bob" />
</div>
);
}
export default App;
2.2 Vue入门
2.2.1 环境搭建
- 安装Node.js和npm。
- 使用Vue CLI创建项目。
npm install -g @vue/cli
vue create my-project
2.2.2 模板语法
Vue使用模板语法来描述UI结构。
<template>
<div>
<h1>Hello, {{ name }}</h1>
</div>
</template>
<script>
export default {
data() {
return {
name: 'Vue'
};
}
};
</script>
2.2.3 组件化开发
Vue支持组件化开发,类似于React。
<template>
<div>
<Welcome msg="Hello, Vue!" />
</div>
</template>
<script>
import Welcome from './components/Welcome.vue';
export default {
components: {
Welcome
}
};
</script>
2.3 Angular入门
2.3.1 环境搭建
- 安装Node.js和npm。
- 使用Angular CLI创建项目。
npm install -g @angular/cli
ng new my-project
2.3.2 TypeScript基础
Angular使用TypeScript作为编程语言,因此需要了解TypeScript的基础语法。
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<h1>Hello, Angular!</h1>`
})
export class AppComponent {}
2.3.3 模块和组件
Angular使用模块(Module)和组件(Component)来组织代码。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
第三部分:实战篇
3.1 React实战
3.1.1 项目结构
my-app/
├── public/
│ └── index.html
├── src/
│ ├── components/
│ │ └── App.js
│ ├── index.js
│ └── styles/
│ └── main.css
└── package.json
3.1.2 路由管理
使用react-router-dom进行路由管理。
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
{/* ...其他路由 */}
</Switch>
</Router>
);
}
export default App;
3.1.3 状态管理
使用Redux进行状态管理。
import { createStore } from 'redux';
import { Provider, useSelector, useDispatch } from 'react-redux';
const initialState = {
count: 0
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'INCREMENT':
return { ...state, count: state.count + 1 };
case 'DECREMENT':
return { ...state, count: state.count - 1 };
default:
return state;
}
};
const store = createStore(reducer);
function App() {
const count = useSelector(state => state.count);
const dispatch = useDispatch();
return (
<Provider store={store}>
<div>
<p>Count: {count}</p>
<button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment</button>
<button onClick={() => dispatch({ type: 'DECREMENT' })}>Decrement</button>
</div>
</Provider>
);
}
export default App;
3.2 Vue实战
3.2.1 项目结构
my-project/
├── public/
│ └── index.html
├── src/
│ ├── assets/
│ │ └── logo.png
│ ├── components/
│ │ └── HelloWorld.vue
│ ├── App.vue
│ ├── main.js
│ └── main.ts
└── package.json
3.2.2 路由管理
使用Vue Router进行路由管理。
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);
const router = new Router({
routes: [
{
path: '/',
name: 'home',
component: () => import('./views/Home.vue')
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
}
// ...其他路由
]
});
new Vue({
router,
render: h => h(App)
}).$mount('#app');
3.2.3 状态管理
使用Vuex进行状态管理。
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++;
},
decrement(state) {
state.count--;
}
},
actions: {
increment({ commit }) {
commit('increment');
},
decrement({ commit }) {
commit('decrement');
}
}
});
new Vue({
store,
render: h => h(App)
}).$mount('#app');
3.3 Angular实战
3.3.1 项目结构
my-project/
├── e2e/
├── node_modules/
├── src/
│ ├── app/
│ │ ├── components/
│ │ │ └── home/
│ │ │ └── home.component.ts
│ │ ├── app.module.ts
│ │ ├── app routing module.ts
│ │ ├── app routing paths.ts
│ │ └── app routing resolver.ts
│ ├── assets/
│ ├── environments/
│ ├── index.html
│ ├── main.ts
│ └── polyfills.ts
└── package.json
3.3.2 路由管理
使用Angular Router进行路由管理。
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
// ...其他路由
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
3.3.3 状态管理
使用NgRx进行状态管理。
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import * as fromApp from '../store/app.reducer';
import * as AppActions from '../store/app.actions';
@Injectable({
providedIn: 'root'
})
export class AppService {
constructor(private store: Store<fromApp.AppState>) {}
get count$() {
return this.store.pipe(select(fromApp.getCount));
}
increment() {
this.store.dispatch(new AppActions.Increment());
}
decrement() {
this.store.dispatch(new AppActions.Decrement());
}
}
第四部分:进阶篇
4.1 React进阶
4.1.1 Hooks
React Hooks允许在不编写类的情况下使用state和副作用。
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
4.1.2 Context API
React Context API允许跨组件传递数据,无需层层传递props。
import React, { createContext, useContext } from 'react';
const CountContext = createContext();
function ParentComponent() {
const count = useContext(CountContext);
return (
<div>
<p>Count: {count}</p>
<ChildComponent />
</div>
);
}
function ChildComponent() {
const count = useContext(CountContext);
return (
<div>
<p>Child Count: {count}</p>
</div>
);
}
4.2 Vue进阶
4.2.1 Composition API
Vue 3.0引入了Composition API,使得组件的编写更加灵活。
import { defineComponent, ref } from 'vue';
export default defineComponent({
setup() {
const count = ref(0);
function increment() {
count.value++;
}
return {
count,
increment
};
}
});
4.2.2 Composition API的响应式引用
Vue 3.0引入了响应式引用(Reactive Refs),可以创建响应式的引用对象。
import { ref } from 'vue';
const countRef = ref(0);
// countRef.value 是响应式的
4.3 Angular进阶
4.3.1 RxJS
Angular使用RxJS库进行异步编程,例如处理HTTP请求。
import { HttpClient } from '@angular/common/http';
function getUser() {
return this.http.get('/api/user');
}
4.3.2 Angular Material
Angular Material是一个UI库,提供了一套丰富的组件和样式。
import { MatTableModule } from '@angular/material/table';
@NgModule({
imports: [MatTableModule]
})
export class TableModule {}
第五部分:总结
React、Vue和Angular是当前最流行的前端框架,每个框架都有其独特的优势和适用场景。在学习过程中,建议从入门篇开始,逐步深入学习每个框架的核心概念和实战技巧。通过不断实践和总结,你将能够熟练地运用这些框架来开发高质量的前端应用。
