前端-vue组件
一、vue生命周期
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
{{ num }}
<br>
<button @click="add">+</button>
</div>
</body>
<script>
let app = new Vue({
el : '#app',
data:{
num : 10
},
beforeCreate() {
console.log('组件创建之前')
},
created() {
console.log('组件创建完成')
},
beforeMount() {
console.log('组件挂载之前')
},
mounted() {
console.log('组件挂载完成')
},
beforeUpdate() {
console.log('组件更新之前')
},
updated() {
console.log('组件更新完成')
},
beforeDestroy() {
console.log('组件销毁之前')
},
destroyed() {
console.log('组件销毁完成')
},
methods: {
add(){
this.num++
}
},
})
app.$destroy()
</script>
</html>
二、v-model修饰符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model.lazy="lazyStr">
<br>
{{ lazyStr }}
<br>
<input type="text" v-model.number="numberStr">
<br>
{{ numberStr + 1}}
<br>
<input type="text" v-model.trim="trimStr">
<br>
{{ trimStr }}
</div>
</body>
<script>
let app = new Vue({
el : '#app',
data : {
lazyStr : '',
numberStr : '',
trimStr : ''
}
})
</script>
</html>
三、vue的数组操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<p v-for="(item,index) in userArr">
{{item.userId}},{{item.userName}},{{item.userSex}}
</p>
<button @click="clear">清空</button>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {
userArr: [
{
userId: 1,
userName: '小明',
userSex: '男'
},
{
userId: 2,
userName: '小红',
userSex: '女'
},
{
userId: 3,
userName: '小丽',
userSex: '女'
}
]
},
methods: {
clear(){
this.userArr.splice(0,this.userArr.length)
}
},
})
</script>
</html>
四、表单绑定
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<h1>注册</h1>
<form>
<label>用户名:</label>
<input type="text" v-model="myForm.userName">
<br>
<label>密码:</label>
<input type="password" v-model="myForm.password">
<br>
<label>确认密码:</label>
<input type="password" v-model="myForm.beginpassword">
<br>
<label>性别:</label>
<input type="radio" v-model="myForm.sex" value="0">男
<input type="radio" v-model="myForm.sex" value="1">女
<br>
<label>爱好:</label>
<input type="checkbox" value="0" v-model="myForm.like">读书
<input type="checkbox" value="1" v-model="myForm.like">体育
<input type="checkbox" value="2" v-model="myForm.like">打游戏
<br>
<label>国籍:</label>
<select v-model="myForm.nationality">
<option value="0">中国</option>
<option value="1">美国</option>
<option value="2">英国</option>
</select>
<br>
<label>个人简介:</label>
<br>
<textarea cols="30" rows="5" v-model="myForm.synopsis"></textarea>
<br>
<input type="button" value="提交" @click="handler">
</form>
</div>
</body>
<script>
let app = new Vue({
el: "#app",
data: {
myForm: {
userName: "",
password: "",
beginpassword: "",
sex: 0,
like: [0, 1, 2],
nationality: 0,
synopsis: ""
}
},
methods: {
handler: function () {
console.log(this.myForm.userName)
console.log(this.myForm.password)
console.log(this.myForm.beginpassword)
console.log(this.myForm.sex)
console.log(this.myForm.like)
console.log(this.myForm.nationality)
console.log(this.myForm.synopsis)
}
}
})
</script>
</html>
五、过滤器
1.全局过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
{{ price | add1 | add2 }}
<br>
{{ num1 | dosomethimg(5,20) }}
<br>
{{ num2 | dosomethimg(15,20) }}
</div>
</body>
<script>
Vue.filter('add1', function (value) {
return '¥' + value
})
Vue.filter('add2', function (value) {
return value + '元'
})
Vue.filter('dosomethimg', function (value, p1, p2) {
if (p1 < 10) {
return value + p1
} else {
return value + p2
}
})
let app = new Vue({
el: '#app',
data: {
message: '全局过滤器',
price: 10,
num1: 5,
num2: 12
}
})
</script>
</html>
2.本地过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
{{ message | Reverse }}
<br>
{{ message | Length }}
</div>
</body>
<script>
let app = new Vue({
el : '#app',
data : {
message : '本地过滤器'
},
filters : {
Reverse : function(value){
if(!value){
return ''
}
return value.toString().split('').reverse().join('')
},
Length : function(value){
return value.length
}
}
})
</script>
</html>
六、vue组件
1.vue组件模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<mycomponent></mycomponent>
</div>
<template id="mytemplate">
<div>
<h1>这是一个布局组件,现在使用组件模板渲染它</h1>
<button @click="reduce">-</button> {{ num }} <button @click="add">+</button>
</div>
</template>
</body>
<script>
let obj = {
template : '#mytemplate',
data() {
return {
num : 1
}
},
methods: {
add(){
this.num++
},
reduce(){
this.num--
}
},
}
let app = new Vue({
el : '#app',
components : {
mycomponent : obj
}
})
</script>
</html>
2.vue全局组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<mycomponent></mycomponent>
</div>
</body>
<script>
Vue.component('mycomponent',{
template :
`
<div>
<h1>这是一个全局组件</h1>
<button @click="reduce">-</button>{{ num }}<button @click="add">+</button>
</div>
`,
data() {
return {
num : 1
}
},
methods: {
add(){
this.num++
},
reduce(){
this.num--
}
},
})
let app = new Vue({
el : '#app',
data : {
}
})
</script>
</html>
3.vue局部组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<mycomponent></mycomponent>
</div>
</body>
<script>
let obj = {
template:
`
<div>
<h1>这是一个局部组件</h1>
<button @click="reduce">-</button>{{ num }}<button @click="add">+</button>
</div>
`,
data() {
return {
num: 1
}
},
methods: {
add() {
this.num++
},
reduce() {
this.num--
}
}
}
let app = new Vue({
el: '#app',
components: {
mycomponent: obj
}
})
</script>
</html>