父组件向子组件传递数据时,在子组件中props如何获取
    通过事件监听watch
    1.在父组件中

    1. <template>
    2. <div>
    3. <child :books="books"></child>
    4. <label for="">id:</label><input type="text" v-model="id" />
    5. <label for="">name:</label><input type="text" v-model="name" />
    6. <button @click="add">添加</button>
    7. </div>
    8. </template>
    9. <script>
    10. import child from "./child.vue";
    11. export default {
    12. name: "",
    13. data() {
    14. return {
    15. books: [
    16. { id: 1, name: "王苏" },
    17. { id: 2, name: "代码" },
    18. ],
    19. id: "",
    20. name: "",
    21. };
    22. },
    23. methods: {
    24. add() {
    25. this.books.push({ id: this.id, name: this.name });
    26. },
    27. },
    28. components: {
    29. child,
    30. },
    31. };
    32. </script>
    33. <style></style>

    2.在子组件中

    1. <template>
    2. <div>
    3. <ul v-for="item in list" :key="item.id">
    4. <li>Id:{{item.id}}</li>
    5. <li>name:{{item.name}}</li>
    6. </ul>
    7. <button @click="del">删除</button>
    8. <son></son>
    9. </div>
    10. </template>
    11. <script>
    12. import son from "./son.vue"
    13. export default {
    14. name:'',
    15. props:{
    16. books:{
    17. type:Array,
    18. default:[]
    19. }
    20. },
    21. data () {
    22. return {
    23. list:[]
    24. }
    25. },
    26. methods: {
    27. del(){
    28. this.list.pop()
    29. }
    30. },
    31. watch:{
    32. books:function(newVal,oldVal){
    33. console.log(newVal)
    34. console.log(oldVal)
    35. this.list=newVal
    36. }
    37. },
    38. components: {
    39. son
    40. }
    41. }
    42. </script>
    43. <style>
    44. </style>

    在子组件中利用watch的事件监听,通过监听props中传过来的属性