首頁  >  文章  >  web前端  >  Vue.js計算屬性computed

Vue.js計算屬性computed

高洛峰
高洛峰原創
2016-12-03 09:11:381187瀏覽

computed的get屬性

html:

<template>
 <div class="input-text">
 <input type="text" v-model=&#39;firstName&#39;>
 <input type="text" v-model=&#39;lastName&#39;>
 {{fullName}}
 </div>
</template>

js:

<script>
export default {
 components: {
 
 },
 ready: function() {
 
 },
 methods: {
 },
 data() {
 return {
 firstName: &#39;Foo&#39;,
 lastName: &#39;Bar&#39;
 }
 },
 computed: {
 fullName: {
 // getter
 get: function() {
 return this.firstName + &#39; and &#39; + this.lastName
 },
 // setter
 set: function(newValue) {
 var names = newValue.split(&#39; and &#39;)
 this.firstName = names[0]
 this.lastName = names[names.length - 1]
 }
 }
 }
}
</script>

computed的get和set屬性:

html:

<template>
 <div class="input-text">
 <input type="text" v-model=&#39;a&#39;>{{b}}
 <input type="button" value="修改b的值" @click=&#39;updateData&#39;>
 <input type="text" v-model=&#39;c&#39;>
 </div>
</template>

js:

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn