• vue-cli之组件之间传值展示数据


    一. 父组件传值到子组件

    (一). 定义一个子组件

    src/components下创建一个Table.vue的组件,内容如下:

    <template>
        <div v-if="item">
            <p>
                <span>{{item.id}}</span>
                <span>{{item.title}}</span>
            </p>
        </div>
    </template>
    
    <script>
        export default {
            name: "Table",
            props: ['item']
        }
    </script>
    
    <style scoped>
    
    </style>
    

    (二). 父组件

    App.vue内容如下:

    <template>
      <div id="app">
        <div>
          <button @click="toggle">切换数据二</button>
          <button @click="toggle1">切换数据二</button>
          <Table :item="item"></Table>
        </div>
      </div>
    </template>
    
    <script>
    import Table from './components/Table' // 引入组件。
    
    export default {
      name: 'App',
      data(){
        return {
          item: {} // 定义一个初始值
        }
      },
      components: {
        Table // 注册组件
      },
      created(){ // 加载页面前就赋值 item
        this.item = {'id': 3, 'title': '默认数据'};
      },
      methods:{
        toggle(){
          console.log('toggle');
          this.item = {'id': 1, 'title': 'aaa'};
        },
        toggle1(){
          console.log('toggle1');
          this.item = {'id': 2, 'title': 'bbb'};
        }
      }
    }
    </script>
    
    <style>
    
    </style>
    

    父组件中使用了<Table :item="item"></Table>使用引入的Table组件。:item对应Table.vue中的props里的某个值。item是传给Table.vue组件的展示数据。

  • 相关阅读:
    汇编 if else
    汇编  cdecl 函数调用约定,stdcall 函数调用约定
    汇编 push ,pop指令
    汇编 EBP ,ESP 寄存器
    汇编 sub减法指令 比较指令CMP JZ条件跳转指令
    thrift使用案例
    基于hiredis,redis C客户端封装
    golang 3des/ecb/cbc/pkcs5 加解密
    ortp 发送RTP实例
    go:基于时间轮定时器方案
  • 原文地址:https://www.cnblogs.com/zhenzi0322/p/14866498.html
Copyright © 2020-2023  润新知