1. Vue.jsbasicconcepts
Vue.js is 一套用于构建user界面 渐进式JavaScriptframework. and 其它 big 型framework不同 is , Vue 被design for 可以自底向 on 逐层application. Vue corelibrary只关注视graph层, 不仅易于 on 手, 还便于 and 第三方library or 既 has project整合.
1.1 Vue.js corefeatures
- response式data绑定: 当data发生变化时, 视graph会自动update; 当视graph发生变化时, data也会自动update.
- component化Development: 将页面拆分成 many 个可复用 component, improvingcode 可maintenance性 and reusability.
- 指令system: providing了丰富 指令, such asv-if, v-for, v-bind, v-onetc., 简化了DOMoperation.
- 虚拟DOM: using虚拟DOMtechniques, improving了页面渲染 performance, reducing了 for 真实DOM operation.
- 生命周期hook: providing了 many 个生命周期hook, 允许Development者 in 不同阶段执行自定义逻辑.
- 过渡 and 动画: in 置了过渡 and 动画system, for application添加流畅 视觉效果.
1.2 Vue.js version
Vue.js目 before has 三个主要version:
- Vue 1.x: 早期version, 已停止maintenance.
- Vue 2.x: stable version, 目 before 广泛using.
- Vue 3.x: 最 new version, providing了更 good performance and new features, such asComposition API, Teleport, Suspenseetc..
提示
for 于初学者, 建议 from Vue 2.x开始Learning, 因 for 它 documentation and resource比较丰富. 当你Master了Vue 2.x after , 可以LearningVue 3.x new features.
2. Vue.jsDevelopmentEnvironment Setup
搭建Vue.jsDevelopmentenvironment has many 种方式, including直接引入, usingVue CLI, usingViteetc..
2.1 直接引入Vue.js
最 simple 方式 is throughCDN直接引入Vue.js. 这种方式适合 fast 速原型Development or Learning.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.jsexample</title>
<!-- 引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{ message }}
</div>
<script>
// creationVueinstance
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
</script>
</body>
</html>
2.2 usingVue CLI
Vue CLI is Vue官方providing 脚手架tool, 可以helping我们 fast 速creationVueproject, configurationDevelopmentenvironment.
2.2.1 installationNode.js and npm
Vue CLI需要Node.js and npm support, 所以 in installationVue CLI之 before , 需要先installationNode.js and npm.
- 访问Node.js官网: https://nodejs.org/zh-cn/
- under 载并installationNode.js (LTSversion)
- verificationinstallation is 否成功:
node -v npm -v
2.2.2 installationVue CLI
usingnpminstallationVue CLI:
npm install -g @vue/cli
verificationinstallation is 否成功:
vue --version
2.2.3 creationVueproject
usingVue CLIcreationVueproject:
vue create my-vue-project
按照提示选择projectconfiguration, 例such as:
- 选择手动选择features
- 选择需要 features, such asBabel, Router, Vuex, CSS Pre-processorsetc.
- 选择Vueversion (2.x or 3.x)
- 选择CSS预processing器 (such asSass/SCSS)
- 选择ESLintconfiguration (such asStandard)
- 选择Babel, ESLintetc.configurationfile 位置 (such asIn dedicated config files)
- is 否保存当 before configuration for 预设 (可选)
2.2.4 启动Developmentserver
进入projectTable of Contents, 启动Developmentserver:
cd my-vue-project
npm run serve
打开浏览器, 访问http://localhost:8080, 即可看 to Vueproject 欢迎页面.
2.3 usingVite
Vite is a new before 端构建tool, 它providing了更 fast Development体验. VitesupportVue 3.x and Vue 2.x.
2.3.1 usingVitecreationVue 3.xproject
npm create vite@latest my-vue3-project -- --template vue
2.3.2 usingVitecreationVue 2.xproject
npm create vite@latest my-vue2-project -- --template vue2
2.3.3 启动Developmentserver
cd my-vue-project
npm install
npm run dev
打开浏览器, 访问http://localhost:5173, 即可看 to Vueproject 欢迎页面.
3. 第一个Vue.jsapplication
现 in , 让我们creation一个 simple Vue.jsapplication, 体验Vue.js basicfunctions.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>第一个Vue.jsapplication</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1>{{ title }}</h1>
<p>{{ message }}</p>
<input type="text" v-model="message" placeholder="请输入 in 容">
<br>
<button v-on:click="changeTitle">modify标题</button>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
title: '第一个Vue.jsapplication',
message: 'Hello Vue!'
},
methods: {
changeTitle: function() {
this.title = 'Vue.jsapplication已update';
}
}
})
</script>
</body>
</html>
in 这个examplein, 我们creation了一个Vueinstance, package含以 under in 容:
- el: 指定Vueinstance挂载 DOM元素.
- data: 定义application data.
- methods: 定义application method.
- {{ }}: using模板语法绑定data.
- v-model: implementation双向data绑定.
- v-on:click: 绑定点击event.
提示
打开浏览器 Development者tool, in 控制台in输入app.message = 'Hello World!';, 你会发现页面 on in 容会立即update, 这就 is Vue.js response式features.
4. Vue.jsDevelopmenttool
Vue.jsproviding了官方 Developmenttool, 可以helping我们debugVueapplication.
4.1 Vue DevTools
Vue DevTools is Vue官方providing 浏览器scale, 可以用于debugVueapplication. 它providing了以 under functions:
- 查看componenttree
- checkcomponent props and data
- monitorevent
- 查看Vuexstatus
- debugVue Router
Vue DevToolssupportChrome, Firefox, Edgeetc.浏览器, 可以 from 各自 scale商店installation.
5. Learningresource
LearningVue.js 一些优质resource:
- Vue.js官方documentation
- Vue 3.x官方documentation
- Vue CLI官方documentation
- Vite官方documentation
- Vuex官方documentation
- Vue Router官方documentation
练习 1: creation simple Vue.jsapplication
- using直接引入 方式creation一个Vue.jsapplication
- implementation一个计数器functions, package含增加, reducing and reset按钮
- usingv-model双向绑定显示当 before 计数
- usingv-on:click绑定按钮event
练习 2: usingVue CLIcreationproject
- installationVue CLI
- usingVue CLIcreation一个Vue 2.xproject
- 启动Developmentserver, 查看project页面
- modifyApp.vuefile, 添加一个 simple 计数器functions