Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之四 vue 基本知識點概述
阿新 • • 發佈:2019-01-06
Vue.js是當下很火的一個JavaScript MVVM庫,它是以資料驅動和元件化的思想構建的。相比於Angular.js,Vue.js提供了更加簡潔、更易於理解的API,使得我們能夠快速地上手並使用Vue.js。
Vue.js(讀音 /vjuː/, 類似於 view) 是一套構建使用者介面的漸進式框架。
Vue 只關注檢視層, 採用自底向上增量開發的設計。
Vue 的目標是通過儘可能簡單的 API 實現響應的資料繫結和組合的檢視組
Hello World示例
瞭解一門語言,或者學習一門新技術,編寫Hello World示例是我們的必經之路。
這段程式碼在畫面上輸出"Hello World!"。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--這是我們的View-->
<div id="app">
{{ message }}
</div>
</body>
<script src="js/vue.js"></script>
<script>
// 這是我們的Model
var exampleData = {
message: 'Hello World!'
}
// 建立一個 Vue 例項或 "ViewModel"
// 它連線 View 與 Model
new Vue({
el: '#app',
data: exampleData
})
</script>
</html>
使用Vue的過程就是定義MVVM各個組成部分的過程的過程。
- 定義View
- 定義Model
- 建立一個Vue例項或"ViewModel",它用於連線View和Model
常用指令
下面的程式碼中,利用v-model進行資料繫結,<template>
<section>
<div v-loading='loading'>
<el-col :span='24' class='toolbar' style='padding-bottom: 0px;'>
<el-form :inline='true'>
<el-form-item>
<el-input v-model='logKeyWord' placeholder='請輸入關鍵字' clearable></el-input>
</el-form-item>
<el-form-item>
<el-button type='primary' v-on:click='getAllLogs'>查詢</el-button>
</el-form-item>
</el-form>
</el-col>
<el-table :data='logLists' stripe style='width: 100%; margin: 0 auto; text-align:center;'>
<el-table-column label='ID'>
<template slot-scope='scope'>
<label>{{scope.row.id}}
<span></span>
</label>
</template>
</el-table-column>
<el-table-column label='時間'>
<template slot-scope='scope'>
<label>{{scope.row.createTime}}
<span></span>
</label>
</template>
</el-table-column>
<el-table-column label='操作'>
<template slot-scope='scope'>
<label>{{scope.row.operateContent}}
<span></span>
</label>
</template>
</el-table-column>
<el-table-column label='操作人'>
<template slot-scope='scope'>
<label>{{scope.row.userId}}
<span></span>
</label>
</template>
</el-table-column>
<el-table-column label='操作結果'>
<template slot-scope='scope'>
<label>{{scope.row.displayName}}
<span></span>
</label>
</template>
</el-table-column>
</el-table>
</div>
</section>
</template>
<style rel='stylesheet/scss' lang='scss'>
.el-table thead tr th {
background-color: rgba(28,148,255,0.6) !important;
color: #fff;
}
.el-table th{
text-align: center;
}
.addBtn {
background: #fff;
color: #1C94FF;
}
</style>
<script lang='babel'>
import webapi from '../../api/webapi'
import {mapState} from 'vuex'
export default {
name: 'log',
data () {
return {
logKeyWord: '',
logLists: [],
loading: false,
textMap: {
update: '編輯',
create: '新增',
delete: '刪除賬號'
},
dialogFormVisible: false,
dialogLoading: false,
temp: {
role: ''
},
dialogStatus: '',
disabledFlag: true // 角色是否可更改
}
},
computed: {
...mapState({
menuList: state => state.menuList.menuList
})
},
mounted () {
this.getAllLogs()
},
methods: {
handleEdit (index, row) {
this.getRoles(row)
this.temp = Object.assign({}, row) // copy obj
this.dialogStatus = 'update'
this.dialogFormVisible = true
},
async handleDelete (index, row) {
try {
let temp = Object.assign({}, row)
const params = {
roleId: temp.id
}
const res = await webapi.manage.roles(params)
if (res.code === '200') {
this.getRoleInfo()
this.$message({
title: '成功',
message: '刪除成功',
type: 'success',
duration: 2000
})
} else {
this.$message({
title: '失敗',
message: res.msg,
type: 'error',
duration: 2000
})
}
} catch (e) {
console.log(e)
}
},
async getAllLogs () {
try {
this.loading = true
const params = {
logKeyWord: this.logKeyWord
}
const res = await webapi.manage.allLogs(params)
if (res.code === '200') {
this.logLists = res.data
} else {
this.logLists = []
}
} catch (e) {
console.log(e)
} finally {
this.loading = false
}
},
itemClick (node) {
console.log('node', JSON.stringify(node.model))
}
}
}
</script>
Vue.js的指令是以v-開頭的,它們作用於HTML元素,指令提供了一些特殊的特性,將指令繫結在元素上時,指令會為繫結的目標元素新增一些特殊的行為,我們可以將指令看作特殊的HTML特性(attribute)。Vue.js提供了一些常用的內建指令,接下來我們將介紹以下幾個內建指令:
- v-if指令
<div slot='footer' class='dialog-footer'>
<el-button @click='dialogFormVisible = false'>取 消</el-button>
<el-button v-if="dialogStatus=='create'" type='primary' @click='createRole'>確 定</el-button>
<el-button v-else type='primary' @click='editRole'>確 定</el-button>
</div>
- v-show指令
v-show
也是條件渲染指令,和v-if指令不同的是,使用v-show
指令的元素始終會被渲染到HTML,它只是簡單地為元素設定CSS的style屬性- v-else指令
- v-for指令
<el-select v-model="child.paramArea" filterable multiple placeholder="請選擇省份" style="width:50%;">
<el-option v-for="item in provinces" :key="item" :label="item" :value="item">
</el-option>
</el-select>
<script lang="babel">
import webapi from '../../../api/webapi'
import {mapState} from 'vuex'
import {provinces} from '../../../utils/config'
import {isAge} from '../../../utils/utils'
- v-bind指令
v-bind
指令可以在其名稱後面帶一個引數,中間放一個冒號隔開,這個引數通常是HTML元素的特性(attribute),例如:v-bind:class
v-bind:argument="expression"
下面這段程式碼構建了一個簡單的分頁條,v-bind指令作用於元素的class特性上。
這個指令包含一個表示式,表示式的含義是:高亮當前頁。此處省略例子
- v-on指令
el-form-item>
<el-button type="primary" v-on:click="getAccountInfo">查詢</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" class='addBtn' @click="handleCreate">新增</el-button>
</el-form-item>
上述兩種寫法都對,第二個是簡寫。Vue例項
main.js檔案定義了vue例項
import Vue from 'vue'
import App from './App'
import {sync} from 'vuex-router-sync'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router'
import store from './store'
import baseComponents from './components'
sync(store, router)
Vue.use(ElementUI)
Vue.config.productionTip = false
Vue.use(baseComponents)
Vue.mixin({
methods: {
$showToast (toast) {
this.$store.dispatch('toastPush', toast)
}
}
})
let vm = new Vue({
el: '#app',//view
router,//model
store,
template: '<App/>',
components: { App }
})
Vue.use({
vm
})
Vue計算屬性,method也可以實現,但是前者利用快取<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試例項 - 菜鳥教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<p>原始字串: {{ message }}</p>
<p>計算後反轉字串: {{ reversedMessage }}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Runoob!'
},
computed: {
// 計算屬性的 getter
reversedMessage: function () {
// `this` 指向 vm 例項
return this.message.split('').reverse().join('')
}
}
})
</script>
</body>
</html>
參考文章:
http://www.runoob.com/vue2/vue-tutorial.html