學(xué)習(xí)心得,
計(jì)算屬性
直接上代碼了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>計(jì)算屬性</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id="example-1-1">
<p>Original message: "{{ msg }}"</p>
<p>Computed reversed message: "{{ fanxaing1 }}"</p>
</div>
<br>
<div id="example-1-2">
<p>Original message: "{{ msg }}"</p>
<p>Computed reversed message: "{{ fanxiang2() }}"</p>
</div>
<hr>
<div id="example-2-1">{{fullName}}</div>
<br>
<div id="example-2-2">{{fullName}}</div>
<hr>
<div id="example-3">
<p>
Ask a yes/no question:
<input type="text" v-model="question">
</p>
<p>{{answer}}</p>
</div>
<hr>
<script>
//計(jì)算屬性,computed:,是有緩存的,用于遍歷消耗嚴(yán)重的數(shù)據(jù),在message沒(méi)有發(fā)生變化時(shí)是不變的.
var vm1 = new Vue({
el:"#example-1-1",
data:{
msg:"Hello~~"
},
computed:{
// 計(jì)算屬性的 getter
fanxaing1:function () {
// `this` 指向 vm 實(shí)例
return this.msg.split('').reverse().join('')
}
}
});
//使用方法來(lái)計(jì)算,method:,沒(méi)有緩存
var vm2 = new Vue({
el:"#example-1-2",
data:{
msg:"123"
},
// 在組件中
methods: {
fanxiang2: function () {
return this.msg.split('').reverse().join('');
}
}
});
//偵聽(tīng)屬性,watch:
var vm3 = new Vue({
el:"#example-2-1",
data:{
firstName:'first~1',
lastName:'last~2',
fullName:'first~last~'
},
watch:{
firstName:function(val){
this.fullName = val +'' +this.lastName
},
lastName:function(val){
this.fullName = this.firstName + '' + val
}
}
});
//當(dāng)你有一些數(shù)據(jù)需要隨著其它數(shù)據(jù)變動(dòng)而變動(dòng)時(shí)齐帚,你很容易濫用 watch,事實(shí)上computed更好
//上面代碼是命令式且重復(fù)的对妄。將它與計(jì)算屬性的版本進(jìn)行比較:
//好得多了敢朱,不是嗎?
var vm4 = new Vue({
el:"#example-2-2",
data:{
firstName:"first~1-2",
lastName:"last~1-2"
},
computed:{
fullName:function(){
return this.firstName + ' , ' +this.lastName;
}
}
});
//計(jì)算屬性的setter
//計(jì)算屬性默認(rèn)只有 getter 拴签,不過(guò)在需要時(shí)你也可以提供一個(gè) setter :
// computed: {
// fullName: {
// // getter
// get: function () {
// return this.firstName + ' ' + this.lastName
// },
// // setter
// set: function (newValue) {
// var names = newValue.split(' ')
// this.firstName = names[0]
// this.lastName = names[names.length - 1]
// }
// }
// }
</script>
<!-- 因?yàn)?AJAX 庫(kù)和通用工具的生態(tài)已經(jīng)相當(dāng)豐富篓吁,Vue 核心代碼沒(méi)有重復(fù) -->
<!-- 提供這些功能以保持精簡(jiǎn)。這也可以讓你自由選擇自己更熟悉的工具杖剪。 -->
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
<script>
var watchExampleVM = new Vue({
el: '#example-3',
data: {
question: '',
answer: '我不能給予你答案,直到你提出問(wèn)題之前!'
},
watch: {
// 如果 `question` 發(fā)生改變盛嘿,這個(gè)函數(shù)就會(huì)運(yùn)行
question: function (newQuestion) {
this.answer = '等待停止輸入...'
this.getAnswer()
}
},
methods: {
// `_.debounce` 是一個(gè)通過(guò) Lodash 限制操作頻率的函數(shù)括袒。
// 在這個(gè)例子中,我們希望限制訪問(wèn) yesno.wtf/api 的頻率
// AJAX 請(qǐng)求直到用戶(hù)輸入完畢才會(huì)發(fā)出芥炭。想要了解更多關(guān)于
// `_.debounce` 函數(shù) (及其近親 `_.throttle`) 的知識(shí)漓库,
// 請(qǐng)參考:https://lodash.com/docs#debounce
getAnswer: _.debounce(
function () {
if (this.question.indexOf('?') === -1) {
this.answer = '問(wèn)題通常都帶有問(wèn)號(hào). ;-)'
return
}
this.answer = '思考中...'
var vm = this
axios.get('https://yesno.wtf/api')
.then(function (response) {
vm.answer = _.capitalize(response.data.answer)
})
.catch(function (error) {
vm.answer = '錯(cuò)誤無(wú)法獲取API. ' + error
})
},
// 這是我們?yōu)榕卸ㄓ脩?hù)停止輸入等待的毫秒數(shù)
500
)
}
})
</script>
</body>
</html>