使用 vue-cli 創(chuàng)建模板項(xiàng)目
- vue-cli 是 vue 官方提供的腳手架工具
- github: https://github.com/vuejs/vue-cli
- 作用: 從 https://github.com/vuejs-templates 下載模板項(xiàng)目
創(chuàng)建 vue 項(xiàng)目
npm install -g vue-cli
vue init webpack vue_demo
cd vue_demo
npm install
npm run dev
訪(fǎng)問(wèn): http://localhost:8080/
模板項(xiàng)目的結(jié)構(gòu)
|-- build : webpack 相關(guān)的配置文件夾(基本不需要修改)
???? |-- dev-server.js : 通過(guò) express 啟動(dòng)后臺(tái)服務(wù)器
|-- config: webpack 相關(guān)的配置文件夾(基本不需要修改)
?????|-- index.js: 指定的后臺(tái)服務(wù)的端口號(hào)和靜態(tài)資源文件夾
|-- node_modules
|-- src : 源碼文件夾
?????|-- components: vue 組件及其相關(guān)資源文件夾
???? |-- App.vue: 應(yīng)用根主組件
?????|-- main.js: 應(yīng)用入口 js
|-- static: 靜態(tài)資源文件夾
|-- .babelrc: babel 的配置文件
|-- .eslintignore: eslint 檢查忽略的配置
|-- .eslintrc.js: eslint 檢查的配置
|-- .gitignore: git 版本管制忽略的配置
|-- index.html: 主頁(yè)面文件
|-- package.json: 應(yīng)用包配置文件
|-- README.md: 應(yīng)用描述說(shuō)明的 readme 文件
示例代碼
/*main.js*/
<template>
<div>
<h2 class="title">{{msg}}</h2>
</div>
</template>
<script>
export default {
data () {
return {
msg: '第一個(gè)Vue組件'
}
}
}
</script>
<style scoped>
.title {
color: red;
}
</style>
/*App.vue*/
<template>
<div>
<img src="./assets/logo.png" alt="logo" class="logo">
<HelloWorld/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
components: {
HelloWorld
}
}
</script>
<style>
.logo {
width: 100px;
height: 100px;
}
</style>
/*HelloWorld.vue*/
<template>
<div>
<h2 class="title">{{msg}}</h2>
</div>
</template>
<script>
export default {
data () {
return {
msg: '第一個(gè)Vue組件'
}
}
}
</script>
<style scoped>
.title {
color: red;
}
</style>
項(xiàng)目的打包與發(fā)布
打包:
npm run build
發(fā)布 1: 使用靜態(tài)服務(wù)器工具包
npm install -g serve
serve dist
訪(fǎng)問(wèn): http://localhost:5000
發(fā)布 2: 使用動(dòng)態(tài) web 服務(wù)器(tomcat)
修改配置: webpack.prod.conf.js
output: {
publicPath: '/xxx/' //打包文件夾的名稱(chēng)
}
重新打包:
npm run build
修改 dist 文件夾為項(xiàng)目名稱(chēng): xxx
將 xxx 拷貝到運(yùn)行的 tomcat 的 webapps 目錄下 訪(fǎng)問(wèn): http://localhost:8080/xxx
eslint
- ESLint 是一個(gè)代碼規(guī)范檢查工具
- 它定義了很多特定的規(guī)則, 一旦你的代碼違背了某一規(guī)則, eslint 會(huì)作出非常有用的提示
- 官網(wǎng): http://eslint.org/
- 基本已替代以前的 JSLint
ESLint 提供以下支持
- ES
- JSX
- style 檢查
- 自定義錯(cuò)誤和提示
ESLint 提供以下幾種校驗(yàn)
- 語(yǔ)法錯(cuò)誤校驗(yàn)
- 不重要或丟失的標(biāo)點(diǎn)符號(hào),如分號(hào)
- 沒(méi)法運(yùn)行到的代碼塊(使用過(guò) WebStorm 的童鞋應(yīng)該了解)
- 未被使用的參數(shù)提醒
- 確保樣式的統(tǒng)一規(guī)則,如 sass 或者 less
- 檢查變量的命名
規(guī)則的錯(cuò)誤等級(jí)有三種
- 0:關(guān)閉規(guī)則。
- 1:打開(kāi)規(guī)則,并且作為一個(gè)警告(信息打印黃色字體)
- 2:打開(kāi)規(guī)則,并且作為一個(gè)錯(cuò)誤(信息打印紅色字體)
相關(guān)配置文件
- .eslintrc.js : 全局規(guī)則配置文件
'rules': {
'no-new': 1
}
- 在 js/vue 文件中修改局部規(guī)則
/* eslint-disable no-new */
new Vue({
el: 'body',
components: { App }
})
-.eslintignore: 指令檢查忽略的文件
*.js
*.vue
組件定義與使用
vue 文件的組成
-模板頁(yè)面
<template>
頁(yè)面模板
</template>
- JS 模塊對(duì)象
<script>
export default {
data() {
return {}
},
methods: {},
computed: {},
components: {}
}
</script>
- 樣式
<style>
樣式定義
</style>
基本使用
- 引入組件
- 映射成標(biāo)簽
- 使用組件標(biāo)簽
<template>
<HelloWorld></HelloWorld>
<hello-world></hello-world>
</template>
<script>
import HelloWorld from './components/HelloWorld'
export default {
components: {
HelloWorld
}
}
</script>
關(guān)于標(biāo)簽名與標(biāo)簽屬性名書(shū)寫(xiě)問(wèn)題
- 寫(xiě)法一: 一模一樣
- 寫(xiě)法二: 大寫(xiě)變小寫(xiě), 并用-連接
組件間通信
組件間通信基本原則
- 不要在子組件中直接修改父組件的狀態(tài)數(shù)據(jù)
- 數(shù)據(jù)在哪, 更新數(shù)據(jù)的行為(函數(shù))就應(yīng)該定義在哪
vue 組件間通信方式
- props
- vue 的自定義事件
- 消息訂閱與發(fā)布(如: pubsub 庫(kù))
- slot
- vuex
組件間通信 1: props
使用組件標(biāo)簽時(shí)
<my-component name='tom' :age='3' :set-name='setName'></my-component>
定義 MyComponent 時(shí)
- 在組件內(nèi)聲明所有的 props
- 方式一: 只指定名稱(chēng)
props: ['name', 'age', 'setName']
- 方式二: 指定名稱(chēng)和類(lèi)型
props: {
name: String,
age: Number,
setNmae: Function
}
- 方式三: 指定名稱(chēng)/類(lèi)型/必要性/默認(rèn)值
props: {
name: {type: String, required: true, default:xxx},
}
注意
- 此方式用于父組件向子組件傳遞數(shù)據(jù)
- 所有標(biāo)簽屬性都會(huì)成為組件對(duì)象的屬性, 模板頁(yè)面可以直接引用
- 問(wèn)題:
? ?a. 如果需要向非子后代傳遞數(shù)據(jù)必須多層逐層傳遞
? ?b. 兄弟組件間也不能直接 props 通信, 必須借助父組件才可以
組件間通信 2: vue 自定義事件
綁定事件監(jiān)聽(tīng)
// 方式一: 通過(guò) v-on 綁定
@delete_todo="deleteTodo"
// 方式二: 通過(guò)$on()
this.$refs.xxx.$on('delete_todo', function (todo) {
this.deleteTodo(todo)
})
觸發(fā)事件
// 觸發(fā)事件(只能在父組件中接收)
this.$emit(eventName, data)
注意:
- 此方式只用于子組件向父組件發(fā)送消息(數(shù)據(jù))
- 問(wèn)題: 隔代組件或兄弟組件間通信此種方式不合適
組件間通信 3: 消息訂閱與發(fā)布(PubSubJS 庫(kù))
訂閱消息
PubSub.subscribe('msg', function(msg, data){})
發(fā)布消息
PubSub.publish('msg', data)
注意
- 優(yōu)點(diǎn): 此方式可實(shí)現(xiàn)任意關(guān)系組件間通信(數(shù)據(jù))
事件的 2 個(gè)重要操作(總結(jié))
- 綁定事件監(jiān)聽(tīng) (訂閱消息)
目標(biāo): 標(biāo)簽元素 <button>
事件名(類(lèi)型): click/focus
回調(diào)函數(shù): function(event){}
- 觸發(fā)事件 (發(fā)布消息)
DOM 事件: 用戶(hù)在瀏覽器上對(duì)應(yīng)的界面上做對(duì)應(yīng)的操作
自定義: 編碼手動(dòng)觸發(fā)
組件間通信 4: slot
- 此方式用于父組件向子組件傳遞"標(biāo)簽數(shù)據(jù)"
子組件: Child.vue
<template>
<div>
<slot name="xxx">不確定的標(biāo)簽結(jié)構(gòu) 1</slot>
<div>組件確定的標(biāo)簽結(jié)構(gòu)</div>
<slot name="yyy">不確定的標(biāo)簽結(jié)構(gòu) 2</slot>
</div>
</template>
父組件: Parent.vue
<child>
<div slot="xxx">xxx 對(duì)應(yīng)的標(biāo)簽結(jié)構(gòu)</div>
<div slot="yyy">yyyy 對(duì)應(yīng)的標(biāo)簽結(jié)構(gòu)</div>
</child>
示例代碼
/*main.js*/
/*
入口JS
*/
import Vue from 'vue'
import App from './App.vue'
import './base.css'
// 創(chuàng)建vm
/* eslint-disable no-new */
new Vue({
el: '#app',
components: {App}, // 映射組件標(biāo)簽
template: '<App/>' // 指定需要渲染到頁(yè)面的模板
})
/*TodoHeader.vue*/
<template>
<div class="todo-header">
<input type="text" placeholder="請(qǐng)輸入你的任務(wù)名稱(chēng),按回車(chē)鍵確認(rèn)" v-model="inputTodo" @keyup.enter="add"/>
</div>
</template>
<script>
export default {
props: {
addTodo: { // 指定屬性名, 屬性值的類(lèi)型, 必要性
type: Function,
required: true
}
},
data () {
return {
inputTodo: ''
}
},
methods: {
add () {
// 得到輸入的數(shù)據(jù)
const inputTodo = this.inputTodo.trim()
// 檢查合法性
if(!inputTodo) {
alert('必須輸入')
return
}
// 封裝一個(gè)todo對(duì)象
const todo = {
title: inputTodo,
complete: false
}
// 添加到todos中顯示
this.addTodo(todo)
// 清除輸入
this.inputTodo = ''
}
}
}
</script>
<style>
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
/*TodoList.vue*/
<template>
<ul class="todo-main">
<TodoItem v-for="(todo, index) in todos" :key="index"
:todo="todo" :deleteTodo="deleteTodo" :index="index"/>
</ul>
</template>
<script>
import TodoItem from './TodoItem.vue'
export default {
// 聲明接收標(biāo)簽屬性
props: ['todos', 'deleteTodo'], // 會(huì)成為當(dāng)前組件對(duì)象的屬性, 可以在模板中直接訪(fǎng)問(wèn), 也可以通過(guò)this來(lái)訪(fǎng)問(wèn)
components: {
TodoItem
}
}
</script>
<style>
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>
/*TodoItem.vue*/
<template>
<li :style="{background: bgColor}" @mouseenter="handleEnter(true)" @mouseleave="handleEnter(false)">
<label>
<input type="checkbox" v-model="todo.complete"/>
<span>{{todo.title}}</span>
</label>
<button class="btn btn-danger" v-show="isShow" @click="deleteItem">刪除</button>
</li>
</template>
<script>
export default {
props: {// 指定屬性名和屬性值的類(lèi)型
todo: Object,
deleteTodo: Function,
index: Number
},
data () {
return {
bgColor: 'white',
isShow: false
}
},
methods: {
handleEnter (isEnter) {
if(isEnter) { // 進(jìn)入
this.bgColor = '#cccccc'
this.isShow = true
} else { // 離開(kāi)
this.bgColor = '#ffffff'
this.isShow = false
}
},
deleteItem () {
this.deleteTodo(this.index)
}
}
}
</script>
<style>
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
</style>
/*TodoFooter.vue*/
<template>
<div class="todo-footer">
<label>
<input type="checkbox" v-model="checkAll"/>
</label>
<span>
<span>已完成{{completeSize}}</span> / 全部{{todos.length}}
</span>
<button class="btn btn-danger" v-show="completeSize" @click="deleteAllCompleted">清除已完成任務(wù)</button>
</div>
</template>
<script>
export default {
props: {
todos: Array,
deleteCompleteTodos: Function,
selectAll: Function
},
computed: {
completeSize () {
return this.todos.reduce((preTotal, todo) => preTotal + (todo.complete?1:0) ,0)
},
checkAll: {
get () { // 決定是否勾選
return this.completeSize===this.todos.length && this.completeSize>0
},
set (value) {// 點(diǎn)擊了全選checkbox value是當(dāng)前checkbox的選中狀態(tài)(true/false)
this.selectAll(value)
}
},
},
methods: {
deleteAllCompleted () {
if(window.confirm('確定清除已完成的嗎?')) {
this.deleteCompleteTodos()
}
}
}
}
</script>
<style>
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>
/*storageUtils.js*/
/*
向local中存儲(chǔ)數(shù)據(jù)的工具模塊
1. 向外暴露一個(gè)函數(shù)(功能)
只有一個(gè)功能需要暴露
2. 向外暴露一個(gè)對(duì)象(包含多個(gè)功能)
有多個(gè)功能需要暴露
*/
const TODOS_KEY = 'todos_key'
export default {
readTodos () {
return JSON.parse(localStorage.getItem(TODOS_KEY) || '[]')
},
saveTodos (todos) {
localStorage.setItem(TODOS_KEY, JSON.stringify(todos))
}
}
/*
export function xxx() {
}
export function yyy () {
}*/