账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何在slot中实现数据传递
    41
    0

    使用背景,在使用vue-admin框架中,使用组件自定义表格时,想在表格中添加一个按钮,通过按钮实现对话框表单,由于功能需求,我需要不同的页面,对话框表单字段不同,所以我需要在框架自带的自定义表单中添加一个slot标签,然后使用时进行复用,但是我使用的过程中,我子组件定义的data,一直报错未定义,我该如何进行修改。
    父组件(多功能表格)一部分代码,我的目的就是在添加一个按钮,但是对于不同页面,按钮文字,展示的对话框表单是不同的:

    <div v-if="searchable && searchPlace === 'top'" class="search-con search-con-top">
      <Select v-model="searchKey" class="search-col">
        <Option v-for="item in columns" v-if="item.key !== 'handle'" :value="item.key" :key="`search-col-${item.key}`">{{ item.title }}</Option>
      </Select>
      <Input @on-change="handleClear" clearable placeholder="输入关键字搜索" class="search-input" v-model="searchValue"/>
      <Button @click="handleSearch" class="search-btn" type="primary"><Icon type="search"/>&nbsp;&nbsp;搜索</Button>
      <slot></slot>
    </div>
    

    子组件(使用多功能表格):

    <template>
      <div>
        <Card>
          <tables ref="tables" editable searchable search-place="top" v-model="tableData" :columns="columns" @on-delete="handleDelete">
          <Button @click="model1 = true" class="search-btn" type="primary">添加</Button>
          </tables>
        </Card>
        <Modal
            v-model="modal1"
            title="Common Modal dialog box title"
            @on-ok="ok"
            @on-cancel="cancel">
            <p>Content of dialog</p>
            <p>Content of dialog</p>
            <p>Content of dialog</p>
        </Modal>
      </div>
    </template>

    子组件data中也定义了model1:

    data () {
        return {
          model1: false,
          columns: [
            { title: '优惠码', key: 'code', sortable: true },
            { title: '规格', key: 'format', editable: true },
            { title: '创建时间', key: 'create_time' },
            {
              title: 'Handle',
              key: 'handle',
              options: ['delete'],
              button: [
                (h, params, vm) => {
                  return h('Poptip', {
                    props: {
                      confirm: true,
                      title: '你确定要删除吗?'
                    },
                    on: {
                      'on-ok': () => {
                        vm.$emit('on-delete', params)
                        vm.$emit('input', params.tableData.filter((item, index) => index !== params.row.initRowIndex))
                      }
                    }
                  })
                }
              ]
            }
          ],
          tableData: [
            {
              'code': 'asdv1321fs',
              'format': '满20减5',
              'create_time': '2018-09-20 09:38:54'
            },
            {
              'code': 'asdv4532fs',
              'format': '满20减5',
              'create_time': '2018-09-20 09:38:54'
            },
            {
              'code': 'asdv1321fs',
              'format': '满20减5',
              'create_time': '2018-09-20 09:38:54'
            },
            {
              'code': 'asdv1321fs',
              'format': '满20减5',
              'create_time': '2018-09-20 09:38:54'
            },
            {
              'code': 'asdv1321fs',
              'format': '满20减5',
              'create_time': '2018-09-20 09:38:54'
            }
          ]
        }
      }

    但是报错:

    Property or method "modal1" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

    我本想使用props解决,但貌似不会使用

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • liQvQil 普通会员 1楼

      在React中,slot通常用于在组件内部传递数据,而不是外部组件。这意味着你在内部组件内部定义了数据,然后在需要使用该数据的地方在该组件内部使用它。

      以下是一个例子:

      ```jsx import React from 'react';

      class MyComponent extends React.Component { constructor(props) { super(props); this.state = { value: 'Hello, world!' }; }

      render() { return (

      Hello, {this.state.value}

      ); } }

      export default MyComponent; ```

      在这个例子中,我们在MyComponent内部定义了一个状态变量value,并在构造函数中初始化了它为'Hello, world!'。然后,在render方法中,我们使用了这个状态变量来设置<h1>的文本。

      请注意,你不能在 slots 中直接访问组件的状态,因为 slot 是一个容器,而不是组件。如果你想在 slot 中访问组件的状态,你需要使用 this.state

      如果你使用的是 slot 的 props,那么你可以这样做:

      ```jsx import React from 'react';

      class MyComponent extends React.Component { render() { return (

      Hello, {this.props.value}

      ); } }

      export default MyComponent; ```

      在这个例子中,我们在MyComponent的props中传递了一个名为value的值。然后,在render方法中,我们使用了这个值来设置<h1>的文本。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部