這篇文章主要為大家展示了“Vue計算屬性是什么”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Vue計算屬性是什么”這篇文章吧。

具體內容如下
①模板內的表達式實際上只用于簡單的運算,對于復雜邏輯,使用計算機屬性。
②基礎例子:
<div id = "example">
<p>Original message:"{{message}}"</p>
<p>Computed reversed message:"{{reversedMessage}}"</p>
</div>var vm = new Vue({
el:"#example",
data:{
message:"Hello"
},
computed:{
//a computed getter
reversedMessage:function(){
//'this' points to the vm instance
return this.message.split('').reverse().join('')
}
}
})這里我們聲明了一個計算機屬性reversedMessage,我們提供的函數將用作屬性vm.reversedMessage的getter。
③計算機緩存 vs Methods
可以通過調用表達式中的method來達到同樣的效果:
<p>Reversed message:"{{reversedMessage}}"</p>//in component
methods:{
reversedMessage:function(){
return this.message.split('').reverse()/join('')
}
}可以將同一個函數定義為一個method而不是一個計算機屬性。對于最終的結果,兩種方式確實是相同的。然而不同的計算機屬性是基于它們的依賴進行緩存的。計算屬性只有在它的相關依賴發生改變時才會重新求值,這就意味著只要message還沒有改變,多次訪問reversedMessage計算屬性會立即返回之前的計算結果,而不必再次執行函數。
下面的計算屬性將不再更新,因為Date.now()不是響應式依賴:
computed:{
now:function(){
return Date.now()
}
}只要發生重新渲染,method調用總會執行該函數。
④computed屬性 vs watch屬性
<div id= "demo">{{fullName}}</div>watch:
var vm = new Vue({
el:"#demo",
data:{
firstName:"Foo",
lastName:"Bar",
fullName:"Foo Bar"
},
watch:{
firstName:function(val){
this.fullName = val + '' + this.lastName
},
lastName:function(val){
this.fullName = this.firstName + '' +val
}
}
})computed:
var vm = new Vue({
el:'#demo',
data:{
firstName:'Foo',
lastName:'Bar'
},
computed:{
fullName:function(){
return this.firstName + ' ' + this.lastName
}
}
})⑤計算setter:
計算屬性默認只有getter,不過在需要是可以提供一個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]
}
}
}
// ...在運行vm.fullName = 'John Doe'時,setter會被調用,vm.firstName和vm.lastName 也相應的會被更新。
⑥觀察watchers
當想要在數據變化相應時,執行異步操作或開銷較大的操作,這是很有用的。
<div id="watch-example">
<p>
Ask a yes/no question:
<input v-model="question">
</p>
<p>{{ answer }}</p>
</div><script src="https://unpkg.com/axios@0.12.0/dist/axios.min.js"></script>
<script src="https://unpkg.com/lodash@4.13.1/lodash.min.js"></script>
<script>
var watchExampleVM = new Vue({
el: '#watch-example',
data: {
question: '',
answer: 'I cannot give you an answer until you ask a question!'
},
watch: {
// 如果 question 發生改變,這個函數就會運行
question: function (newQuestion) {
this.answer = 'Waiting for you to stop typing...'
this.getAnswer()
}
},
methods: {
// _.debounce 是一個通過 lodash 限制操作頻率的函數。
// 在這個例子中,我們希望限制訪問yesno.wtf/api的頻率
// ajax請求直到用戶輸入完畢才會發出
// 學習更多關于 _.debounce function (and its cousin
// _.throttle), 參考: https://lodash.com/docs#debounce
getAnswer: _.debounce(
function () {
var vm = this
if (this.question.indexOf('?') === -1) {
vm.answer = 'Questions usually contain a question mark. ;-)'
return
}
vm.answer = 'Thinking...'
axios.get('https://yesno.wtf/api')
.then(function (response) {
vm.answer = _.capitalize(response.data.answer)
})
.catch(function (error) {
vm.answer = 'Error! Could not reach the API. ' + error
})
},
// 這是我們為用戶停止輸入等待的毫秒數
500
)
}
})
</script>在這個示例中,使用watch選項允許我們執行異步操作,限制我們執行該操作的頻率,并在得到最終結果前,設置中間狀態,這是計算屬性無法做到的。
以上是“Vue計算屬性是什么”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯網站建設公司行業資訊頻道!
另外有需要云服務器可以了解下創新互聯建站www.yijiale78.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:Vue計算屬性是什么-創新互聯
文章網址:http://www.yijiale78.com/article14/dicsde.html
成都網站建設公司_創新互聯,為您提供搜索引擎優化、服務器托管、Google、網站導航、App開發、App設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯