Vue实例—— 计算属性(computed)、 watch 状态监听和filter 过滤器

Vue实例—— 计算属性(computed)、 watch 状态监听和filter 过滤器

Vue实例配置对象

选项 说明
data Vue实例数据对象
methods 定义Vue实例中的方法
components 定义子组件
computed 计算属性
filters 过滤器

计算属性(computed)

格式:computed:{ 变量(){ 计算表达式} }

<p>总价格为:{{sum}}</p>


var app=new Vue({
el:"#app",
data:{
price:10,
num:0
},
//按钮事件自增,自减事件
menthods:{
  add:function(){
      this.num++;
  },
   end:function(){
        this.num--;
   },
},
    computed:{
      sum(){
          return this.price*this.num
      }
    }

})

watch 状态监听(用来监听事件的变化)

注意:监听名要与被监听名相同,监听上一个数据,和新的数据。(应用场景:数据发生改变需要保存新数据或者需要查看以前的数据)

<!-- -->
<body>
    <div id="app">
        <input type="text" v-model="name"><br>
        <input type="text" v-model="home">
    </div>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                name: "上海",
                home:"北京"
            },
            watch: {
                name(newvalue, oldvaule) {
                    console.log("新城市:" + newvalue)           //新的值
                    console.log("旧的城市:" + oldvaule)           //旧的值
                },
                    home(newvalue,oldvaule){
                    console.log("新家:" + newvalue)    //新的值
                    console.log("旧家:" + oldvaule)    //旧的值
                    }
                },
     
        });

    </script>
</body>
     watch: {  
     //简写
     name(newvalue, oldvaule) {
    console.log("新城市:" + newvalue)           //新的值
     console.log("旧的城市:" + oldvaule)           //旧的值
                },
      //全写
      name:{
      immediate:true,  //是否初始化时执行 
       handler(newvalue,oldvalue){
      console.log("新城市:" + newvalue)           //新的值
      console.log("旧的城市:" + oldvaule)           //旧的值
                  }
                }
     }

filter 过滤器

filter 过滤器,(对数进行格式化,比如字符串首字母大写,日期格式化等据)在页面中直接操作数据,返回最终结果。

<body>
    <div id="app">
        {{mis | bag}}
    </div>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
              mis: "hello world",         //小写转大写
            },
        filters:{
         bag(value){
         return  value?value.toUpperCase():"" 

         }   
        }
          
        });

    </script>
</body>
posted @   永恒之月TEL  阅读(69)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 Welcome Home REOL
  2. 2 Everywhere We Go REOL
  3. 3 凄美地 REOL
  4. 4 最后一页 REOL
Everywhere We Go - REOL
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : 陈冠希/MC仁/厨房仔

作曲 : Wyshmaster

编曲 : Wyshmaster

应采儿:

应采儿:

去到每一度 点解总会有得嘈

难度继续困死阴湿小气岛

我有一路 清楚找我有幅图

闲话素来任你讲卡都好储

厨房仔:

走 去一个冇压力嘅地方

尽情释放 唔驶理人哋眼光

黑色白色 唔驶睇人面色

意识空间 拥有无穷面积

日出 日落 升空 降落

一幕又一幕 如梦生命片段重现

边一幕最值得回味快乐

世世代代 一个一个部落起落

边度着落 降临福地

地球人 已经忘记 幸福 气味

太空旅程 一步内 由呢度去到个度

两道光线引导 启动星际航导

随意门 去边度就边度

感应天路 走佬

应采儿:

On a boat on a train or in an airplane

There's nothing I'd rather do

When we fly in the sky and we go so high

I've got the world with you

去到每一度 点解总会有得嘈

难度继续困死阴湿小气岛

我有一路 清楚找我有幅图

闲话素来任你讲卡都好储

陈冠希:

细个个阵时日日喺度发梦

老师话我长大之后一定冇用

你咁嘅态度 我戥你老豆老母阴公

但系今时今日 超西飞喺天空

飞 飞到洛杉矶

飞飞飞 继续超越自己

而家飞飞飞度边度都似我屋企

同我之前啲老师

我而家举起我个杯

我冇放喺心内向你敬礼

一齐猜猜个枚

247 玩成晚我同啲兄弟

唔会嗌攰

代表 clot 你可以叫我地 clot crew

巴黎东京纽约马尔代夫

感觉世界好闷

细个发啲梦开始变现实

除咗佢我个世界争咩 无乜

应采儿:

On a boat on a train or in an airplane

There's nothing I'd rather do

When we fly in the sky and we go so high

I've got the world with you

去到每一度 点解总会有得嘈

难度继续困死阴湿小气岛

我有一路 清楚找我有幅图

闲话素来任你讲卡都好储

MC仁:

准备包袱走路 一步一步计算好

冇最后说话 最后手稿

冇嘢要透露 冇人走宝

掉埋包袱走佬 冇论乜嘢地步

不理疲劳

天与地当被铺 冇任务 几咁好

应采儿:

On a boat on a train or in an airplane

There's nothing I'd rather do

When we fly in the sky and we go so high

I've got the world with you

去到每一度 点解总会有得嘈

难度继续困死阴湿小气岛

我有一路 清楚找我有幅图

闲话素来任你讲卡都好储

去到每一度 点解总会有得嘈

难度继续困死阴湿小气岛

我有一路 清楚找我有幅图

闲话素来任你讲卡都好储