其他分享
首页 > 其他分享> > vue侦听属性和计算属性

vue侦听属性和计算属性

作者:互联网

监听movies,实现点击添加显示到li标签里面。页面效果如下:

 

<template>
  <div>
    <div class="moive">
      <ul class="list">
        <li v-for="item in fullMovies">{{ item }}</li>
      </ul>
      <button v-on:click="addMovie">add movie</button>
    </div>
  </div>
       
</template>

<script>
    export default {
        name: 'test2',
        data() {
            return {
              movies: [{
                name: '捉妖记2',
                year: 2018
              },
              {
                name: '红海行动',
                year: 2017
              },
              {
                name: '速度与激情',
                year: 2017
              }
            ]
          }
        },
        computed: {
          fullMovies: {
            get: function() {
              var arr = [];
              for (var i = 0; i < this.movies.length; i ++) {
                arr.push(this.movies[i].name + '(' + this.movies[i].year + ')'); // 速度与激情(2017)
              }
              return arr;
            }
          }
        },
        methods: {
          addMovie: function() {
            this.movies.push({
              name: '妖猫传3',
              year: 2019
            })
          }
        },
        watch: {
          movies: function(newValue) {
            alert('我添加了' + newValue[newValue.length - 1].name);
          }
        }
      }
</script>

<style scoped>

</style>

 

标签:function,arr,vue,name,movies,year,2017,侦听,属性
来源: https://www.cnblogs.com/renqiaoqiao/p/11788693.html