提示:主要内容来自老杜的Vue视频教学,大家可以去学习!!!
注意:如果大家看我的文章的话记得直接将代码复制过去运行,然后前后对比学习。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字符串反转案例title>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
输入的数据:<input type="text" placeholder="请输入您需要反转的数据" v-model="data"> <br>
反转的数据:{{data.split('').reverse().join('')}}
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg: '字符串反转案例',
data: ''
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>methods 计算属性----实现字符串的反转title>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
输入的数据:<input type="text" placeholder="请输入您需要反转的数据" v-model="info"> <br>
反转的数据:{{reversedData()}} <br>
{{reversedData()}} <br>
div>
<script>
let count = 0
const vm = new Vue({
el : '#app',
data : {
msg: 'methods 实现字符串反转',
info: ''
},
methods: {
reversedData(){
count++;
return this.info.split('').reverse().join('')
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算属性实现字符串反转title>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
原始输入数据:<input type="text" placeholder="请输入您需要反转的数据" v-model="info"> <br>
反转后的数据:{{reversedData}} <br>
{{reversedData}} <br>
{{reversedData}} <br>
{{reversedData}} <br>
{{reversedData}} <br>
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg: '计算属性----实现字符串的反转',
info: ''
},
computed:{
// reversedData:{
// get(){
// console.log('计算属性的 getter方法执行了!!!')
// return this.info.split('').reverse().join('')
// },
// set(){}
// }
//计算属性的简写形式,注意这里看起来和函数是一模一样的但是这是一个计算属性
reversedData(){
return this.info.split('').reverse().join('')
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>监听属性的变化title>
<script src="../js/vue.js">script>
head>
<body>
<div id='app'>
<h1>{{msg}}h1>
姓名:<input type="text" placeholder="请输入姓名" v-model="stu.name"> <br>
年龄:<input type="text" placeholder="请输入年龄" v-model="stu.age"> <br>
{{stu.profile}}
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg: '监听属性的变化',
stu: {
name: 'jackson',
age: 18,
profile: ''
}
},
watch: {
stu:{
deep: true, //监听整个对象,如果对象中任何东西变化就会执行 handler 中的代码片段
immediate: true,
handler(newValue, oldValue){
this.stu.profile = '我叫' + this.stu.name + ",今年" + this.stu.age + '岁了。'
}
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>比较大小案例title>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
数值1:<input type="number" v-model="number1"> <br>
数值2:<input type="number" v-model="number2"> <br>
比较的结果:{{comparedInfo}}
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg: 'computed实现比较大小案例',
number1: 0,
number2: 0
},
computed: {
comparedInfo(){
res = this.number1 - this.number2
if (res == 0)
return this.number1 + '=' + this.number2
return res>0 ? this.number1 + '>' + this.number2 : this.number1 + '<' + this.number2
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>比较大小案例title>
<script src="../js/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
数值1:<input type="number" v-model="number1"> <br>
数值2:<input type="number" v-model="number2"> <br>
比较的结果:{{comparedInfo}}
div>
<script>
const vm = new Vue({
el : '#app',
data : {
msg: 'watch实现比较大小案例',
number1: 0,
number2: 0,
comparedInfo: ''
},
watch: {
//不使用简写的方式
number1:{
immediate: true,
handler(newValue, oldValue){
res = newValue - this.number2
if(res == 0)
this.comparedInfo = newValue + '=' + this.number2
else if(res > 0)
this.comparedInfo = newValue + '>' + this.number2
else
this.comparedInfo = newValue + '<' + this.number2
}
},
//使用简写的方式
number2(newValue, oldValue){
res = newValue - this.number1
if(res == 0)
this.comparedInfo = this.number1 + '=' + newValue
else if(res > 0)
this.comparedInfo = this.number1 + '<' + newValue
else
this.comparedInfo = this.number1 + '>' + newValue
}
}
})
script>
body>
html>
其他参考文章以及文章部分内容的来源
来源:老杜的视频讲解