- 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"/> 搜索</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 条
- 全部回答
-
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>的文本。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
