首页 > 其他 > 详细

[Vue基础实战]getter及setter的使用

时间:2021-01-18 09:36:13      阅读:39      评论:0      收藏:0      [点我收藏+]

参考代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>getter及setter的使用</title>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="title" />
      <input type="text" v-model="name" />
      <input type="button" value="修改计算属性" @click="changName">
    </div>
    <script src="../js/vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          title: "aaaa",
          name: "bbbb",
        }, 
        methods: {
          changName(){
            this.fullName="cccc-dddd";
          }
        },
        computed:{
          fullName:{
            get:function(){
              return this.title+this.name;

            },
            set:function(newVal){
              var parts=newVal.split(-);
              this.title=parts[0];
              this.name=parts[1];
            }
          }
        }      

      });
    </script>
  </body>
</html>

 

[Vue基础实战]getter及setter的使用

原文:https://www.cnblogs.com/dshow/p/14290766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!