Web前端-Vue.js必備框架(一)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue js</title>
</head>
<body>
<div id="app">
<h1>{{ message }}</h1>
<input type="text" v-model="message">
<pre>
{{ $data | json }}
</pre>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: "#app",
data: {
message: "Hello World!"
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue js</title>
</head>
<body>
<div id="app">
<form>
<div class="error" v-show="!message">dashucoding</div>
<textarea v-model="message"></textarea>
<button v-show="message">Send</button>
</form>
<pre>{{ $data | json }}</pre>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: '#app',
data:{
message: ''
}
});
</script>
</body>
</html>
v-show
和v-if
使用第一個整體元素還存在,使用第二個整體都不存在了。
<head>
<meta charset="UTF-8">
<title>Vue.js</title>
<link rel = "stylesheet" href="main.css">
</head>
<body>
<div id = "app">
<form action="demo.html" v-on:submit="submitForm">
<button type="submit">Submit</button>
</form>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: '#app',
methods: {
submitForm: function(e){
alert("dashucoding");
//e.preventDefault();
}
}
})
</script>
</body>
</html>
組件化
<body>
<div id="app">
<count></count>
</div>
<template id="da">
</template>
<script src="vue.min.js"></script>
<script>
Vue.component("counter", {
template: '<h1>dashu</h1>'
});
new Vue({
el: '#app',
data: {
count: 0
},
})
</script>
</body>
什么是Vue.js
?是目前最火的框架,React
是最流行的框架,打包工具Webpack
,目前三大主流Vue.js
,Angular.js
,React.js
框架。
Vue.js
構建用戶界面框架,注重視圖層。
React
開發網站,開發手機APP
Vue
可以用於開發APP,需要Weex
MVC 是 后端開發的概念
MVVM,視圖層分離
Model, View, VM ViewModel
MVVM
的案例:
v-cloak
[v-clock]{
display: none;
}
<p v-cloak></p>
v-text v-html v-bind: v-on
事件修飾符
v-on
.stop 阻止冒泡
.prevent 阻止默認事件
.capture 使用事件捕獲模式
.self 只當事件在該元素本身觸發時回調
.once 事件只觸發一次
<body>
<div id = "app">
<div>
<input type="button" value="戳他">
<div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {},
methods: {}
});
</script>
</body>
實例:Vue.js
只關注視圖層
<div id = "app">
<p> {{ message }} </p>
</div>
下載地址:
https://vuejs.org/js/vue.min.js
<div id="app">
{{ message }}
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
<div id="app">
<span v-bind:title="message">
鼠標懸停
</span
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
<div id="app">
<p v-if="seen">看到我了</p>
</div>
var app = new Vue({
el: '#app',
data: {
seen: true
}
})
條件循環
<div id="app">
<ol>
<li v-for="do in dos">
{{ do.text }}
</li>
</ol>
</div>
var app = new Vue({
el: '#app',
data: {
dos: [
{ text: 'JavaScript' },
{ text: 'Vue' },
{ text: '項目' }
]
}
})
// v-on 添加一個事件監聽器
<div id="app">
<p>{{ message }}</p>
<button v-on:click="Message">消息</button>
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue.js'
},
methods: {
Message: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
<div id="app">
<p>{{ message }}</p>
<input v-model="message">
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
組件
Vue.component('do-item', {
template: '<li>dashucoding</li>'
})
<do-item></do-item>
<div id="app">
<ol>
<do-item
v-for="item in List"
v-bind:todo="item"
v-bind:key="item.id"
></do-item>
</ol>
</div>
var app = new Vue({
el: '#app',
data: {
List: [
{ id: 0, text: '1' },
{ id: 1, text: '2' },
{ id: 2, text: '吃的東西' }
]
}
})
計算器:
<div id="app">
<input type = "text" v-model = "n1">
<select v-model="opt">
<option value="0"> + </option>
<option value="1"> - </option>
<option value="2"> × </option>
<option value="3"> ÷ </option>
</select>
<input type="text" v-model="n2">
<input type="button" value="=" v-on:click="getResult">
<input type="text" v-model="result">
</div>
添加class類樣式
<h1 :class="[ 'red', 'yellow' ]" > </h1>
<h1 :class=" [ 'red', 'than', isactive? 'active' : '' ]">
<h1 :class="[ 'red', 'than', {'active' : isactive}]"></h1>
style
<h1 :style="{color: 'red', 'font-size' : '20px'}"></h1>
v-for
<div id ="app">
//<p>{{list[0]}}</p>
<p v-for="item in list"> {{item}} </p>
<p v-for="(item,i) in list"> {{item}} </p>
</div>
<p v-for="count in 10">{{count}}</p>
v-if 不斷創建和刪除,不利於重復使用渲染
v-show 不會刪除DOM
v-cloak v-text v-bind v-on
v-bind :
v-on @
v-model : 表單元素
v-for
v-if
v-show
// 事件修飾符
.stop .prevent
.capture
.self
.once
v-for 使用key屬性 string/number
創建一個Vue
var vm = new Vue({
})
var data={a:1};
var vm = new Vue({
data: data
})
vm.a == data.a // true
生命周期圖示
數據綁定
<span> {{msg}} </span>
v-once執行一次
<span v-once> {{msg}} </span>
<!-- 完整語法 -->
<a v-bind:href="url">...</a>
<!-- 縮寫 -->
<a :href="url">...</a>
<!-- 完整語法 -->
<a v-on:click="1">...</a>
<!-- 縮寫 -->
<a @click="1">...</a>
目錄 | 說明 |
---|---|
build |
項目構建 |
config |
配置目錄 |
mode_modules |
npm 加載的項目依賴模塊 |
src |
開發的目錄 |
static |
靜態資源目錄 |
test |
初始測試目錄 |
index.html |
入口文件 |
package.json |
項目配置文件 |
實例:
<div id="vue_">
<h1>{{site}}</h1>
<h1>{{url}}</h1>
<h1>{{det()}}</h1>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#vue_',
data: {
site: "123",
url: ".com"
},
methods: {
det: function() {
return this.site;
}
}
})
</script>
v-html="message"
v-bind:class="{'class1': use}"
v-bind:id="id"
<script>
new Vue({
el: '#app',
data: {
id : 1
}
})
</script>
v-if="seen"
<pre><a v-bind:href="url">123</a></pre>
<script>
new Vue({
el: '#app',
data: {
url: '123456'
}
})
</script>
<div id="app">
<p>{{ message }}</p>
<input v-model="message">
</div>
<script>
new Vue({
el: '#app',
data: {
message: '123456!'
}
})
</script>
v-if v-else v-else-if
<div id="app">
<ul>
<li v-for="n in 10">
{{ n }}
</li>
</ul>
</div>
<div id="app">
{{ message.split('').reverse().join('') }}
</div>
Vue
代碼
<body>
<div id="app">
<p>{{ msg }}</p>
</div>
<script>
// vm 對象就是 MVVM中的 VM調度者
var vm = new Vue({
el: '#app', // 表示當前要控制頁面上的哪個區域
// data 就是 MVVM中的 M
data: {
msg: '歡迎'
}
})
</script>
</body>
v-cloak
,v-bind:
,v-on:
學習
<body>
<div id="app">
<!-- v-cloak 解決 插值表達式閃爍的問題 -->
<p v-cloak> {{ msg }} </p>
<h4 v-text="msg">1</h4>
<!-- v-text 沒有閃爍問題 -->
<!-- v-text會覆蓋元素中原本的內容,插值表達式不會把整個元素的內容清空 -->
<div>{{msg2}}</div>
<div v-text="msg2"></div>
<div v-html="msg2">111</div>
<!-- v-bind: 提供綁定屬性的指令 -->
<!-- <input type="button" value="按鈕" v-bind:title="my"> -->
<!-- v-on: 事件綁定機制 -->
<!-- <input type="button" value="按鈕" :title="my" v-on:click="alert('hello')"> -->
<input type="button" value="按鈕" @click="show">
</div>
<script src="./lib/vue-2.4.0.js"></script>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: '123',
msg2: '<h1>1111</h1>',
my: '11111'
},
methods: {
show: function () {
alert('Hello')
}
}
})
</script>
</body>
倒序效果
<body>
<!-- 2. 創建一個要控制的區域 -->
<div id="app">
<input type="button" value="1" @click="lang">
<input type="button" value="2" @click="stop">
<h4>{{ msg }}</h4>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: 'dashu',
intervalId: null
},
methods: {
lang() {
if (this.intervalId != null) return;
this.intervalId = setInterval(() => {
var start = this.msg.substring(0, 1)
var end = this.msg.substring(1)
this.msg = end + start
}, 600)
},
stop() {
clearInterval(this.intervalId)
this.intervalId = null;
}
}
})
</script>
</body>
事件修飾符
.stop
阻止冒泡
.prevent
阻止默認行為
.capture
實現捕獲事件的機制
.self
實現只點擊當前元素,才會觸發事件
.once
只觸發一次事件
v-model
指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<h4>{{ msg }}</h4>
<input type="text" style="width:100%;" v-model="msg">
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: '123'
},
methods: {
}
});
</script>
</body>
</html>
計算器
var Str = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'
this.result = eval(Str)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="n1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" v-model="n2">
<input type="button" value="=" @click="calc">
<input type="text" v-model="result">
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
n1: 0,
n2: 0,
result: 0,
opt: '+'
},
methods: {
calc() {
switch (this.opt) {
case '+':
this.result = parseInt(this.n1) + parseInt(this.n2)
break;
case '-':
this.result = parseInt(this.n1) - parseInt(this.n2)
break;
case '*':
this.result = parseInt(this.n1) * parseInt(this.n2)
break;
case '/':
this.result = parseInt(this.n1) / parseInt(this.n2)
break;
}
}
}
});
</script>
</body>
</html>
vue
樣式
<h1 class="red"></h1>
<h1 :class=" [ 'red', 'green' ] "></h1> 綁定數組
<h1 :class=" [ 'red', 'green', flag?'active':'' ] "></h1> 三元表達式
<h1 :class="classObj">123</h1>
<script>
var vm = new Vue({
el: '#app',
data: {
flag: true,
classObj: { red: true, green: true, active: false }
},
methods: {}
});
</script>
樣式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<!-- <h1 :style="ss">12</h1> -->
<h1 :style="[ ss, ee ]">123</h1>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
ss: { color: 'red', 'font-weight': 200 },
ee: { color: 'red' }
},
methods: {}
});
</script>
</body>
</html>
for
循環
// 數組對象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<p v-for="(item, i) in list">{{ item.id }} - {{ item.name }} - {{i}}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
list: [
{ id: 1, name: 'd' },
{ id: 2, name: 'dd' },
{ id: 3, name: 'ddd' },
{ id: 4, name: 'dddd' }
]
},
methods: {}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<p v-for="(item, i) in list">{{i}} - {{item}}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
list: [1, 2, 3, 4 ]
},
methods: {}
});
</script>
</body>
</html>
// 循環對象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<p v-for="(val, key, i) in user">{{ val }} -- {{key}} -- {{i}}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
user: {
id: 1,
name: 'da',
gender: '男'
}
},
methods: {}
});
</script>
</body>
</html>
// 迭代數字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dashu</title>
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<p v-for="count in 6"> {{ count }} </p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {},
methods: {}
});
</script>
</body>
</html>
key
屬性使用
<p v-for="item in list" :key="item.id">
<input type="checkbox">{{item.id}} --- {{item.name}}
</p>
add() {
this.list.unshift({ id: this.id, name: this.name })
}
v-if
每次都會重新刪除和創建元素,性能消耗高
v-show
每次不會重新創建,初始渲染消耗高
vue
中綁定樣式兩種方法:
v-bind: class v-bind:style
結言
好了,歡迎在留言區留言,與大家分享你的經驗和心得。
感謝你學習今天的內容,如果你覺得這篇文章對你有幫助的話,也歡迎把它分享給更多的朋友,感謝。
作者簡介
達叔,理工男,簡書作者&全棧工程師,感性理性兼備的寫作者,個人獨立開發者,我相信你也可以!閱讀他的文章,會上癮!,幫你成為更好的自己。長按下方二維碼可關注,歡迎分享,置頂尤佳。