Vue實(shí)例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue實(shí)例</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="root">
<div @click="handleClick">
{{message}}
</div>
<item></item>
</div>
<script type="text/javascript">
Vue.component('item', {
template: '<div>hello item</div>'
})
var vm = new Vue({ // 根實(shí)例
el: '#root', // 綁定元素
data: { // 數(shù)據(jù)
message: "hello world"
},
methods: { // 方法
handleClick: function(){
alert("hello")
}
}
})
</script>
</body>
</html>
Vue實(shí)例生命周期函數(shù)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue實(shí)例生命周期函數(shù)</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
</div>
<script type="text/javascript">
// 生命周期函數(shù)就是vue實(shí)例在某一個(gè)時(shí)間點(diǎn)會自動(dòng)執(zhí)行的函數(shù)
var vm = new Vue({
el: '#app',
template: "<div>{{test}}</div>",
data: {
test: "hello world"
},
beforeCreate: function(){
console.log("beforeCreate")
},
created: function(){
console.log("created")
},
beforeMount: function(){
console.log(this.$el)
console.log("beforeMount")
},
mounted: function(){
console.log(this.$el)
console.log("mounted")
},
beforeDestroy: function(){
console.log("beforeDestroy")
},
destroyed: function(){
console.log("destroyed")
},
beforeUpdate:function(){
console.log("beforeUpdate")
},
updated:function(){
console.log("updated")
}
})
</script>
</body>
</html>
Vue模板語法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue模板語法</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- 差值表達(dá)式 -->
<div>{{name + ' Lee'}} </div>
<!-- v-text 和差值表達(dá)式效果相同 -->
<div v-text="name + ' Lee'"></div>
<div v-html="name + ' Lee'"></div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
name: "Dell"
}
})
</script>
</body>
</html>
Vue計(jì)算屬性脱惰、方法妹蔽、偵聽器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue計(jì)算屬性马澈、方法、偵聽器</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- {{firstName + " " + lastName}} -->
<!-- 計(jì)算屬性 -->
<!-- {{fullName}} -->
<!-- {{age}} -->
<!-- 方法 -->
<!-- {{fullName()}} -->
<!-- {{age}} -->
<!-- 偵聽器 -->
{{fullName}}
{{age}}
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
firstName: "Dell",
lastName: "Lee",
fullName: 'Dell Lee',
age: 28
},
// 計(jì)算屬性
// computed: { // 存在緩存機(jī)制贪嫂,計(jì)算的屬性無變化時(shí)不會重新計(jì)算
// fullName: function() {
// console.log("計(jì)算了一次")
// return this.firstName + " " + this.lastName
// }
// },
// 方法
// methods:{
// fullName: function(){ // 無緩存機(jī)制
// console.log("執(zhí)行了一次")
// return this.firstName + " " + this.lastName
// }
// },
// 偵聽器
watch:{
firstName: function(){
console.log("偵聽了一次")
this.fullName = this.firstName + " " + this.lastName
},
lastName: function(){
console.log("偵聽了一次")
this.fullName = this.firstName + " " + this.lastName
}
}
})
</script>
</body>
</html>
Vue計(jì)算屬性的setter和getter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue計(jì)算屬性的setter和getter</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
{{fullName}}
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
firstName: "Dell",
lastName: "Lee",
},
computed: {
fullName: {
get: function(){
return this.firstName + " " + this.lastName
},
set: function(value){
var arr = value.split(" ")
this.firstName = arr[0]
this.lastName = arr[1]
}
}
}
})
</script>
</body>
</html>
Vue中的樣式綁定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的樣式綁定</title>
<script type="text/javascript" src="./vue.js"></script>
<style type="text/css">
.activated{
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <div @click="handleDivClick"
:class="[activated, activatedOne]"
>
Hello world
</div> -->
<div :style="[styleObj,{fontSize: '20px'}]" @click="handleDivClick">
Hello world
</div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data:{
styleObj: {
color: ""
}
// activated: "",
// activatedOne: "activated-one"
},
methods:{
// handleDivClick: function(){
// // if(this.activated === "activated"){
// // this.activated = ""
// // }else{
// // this.activated = "activated"
// // }
// this.activated = this.activated === "activated" ?
// "" : "activated"
// }
handleDivClick: function(){
this.styleObj.color = this.styleObj.color === "red" ?
"":"red"
}
}
})
</script>
</body>
</html>
Vue中的條件渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的條件渲染</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- <div v-if="show === 'a'" data-test="v-if">This is a</div>
<div v-else-if="show === 'b'" data-test="v-if">This is b</div>
<div v-else>This is others </div> -->
<div v-if="show">
用戶名:<input type="" name="" key="username">
</div>
<div v-else>
郵箱名:<input type="" name="" key="password">
</div>
<!-- display: none v-show性能高 -->
<!-- <div v-show="show" data-test="v-show">{{message}}</div> -->
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data:{
show: "a",
message: "Hello world"
},
})
</script>
</body>
</html>
Vue中的列表渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的列表渲染</title>
<script type="text/javascript" src="./vue.js"></script>
</head>
<body>
<div id="app">
<div v-for="item of userInfo">
{{item}}
</div>
</div>
<script type="text/javascript">
// push pop shift unshift splice sort reverse
var vm = new Vue({
el: '#app',
data:{
userInfo: [
1,2,3,4
]
},
})
</script>
</body>
</html>