背景:因弹出层多个页面的详情都是一样的,因此把弹出层定义成组件,多次调用
定义组件的过程中出现很多问题,因此再次记录最终成功的写法
- <template>
- <el-dialog title="详情" :visible.sync="dialogTableVisible" >...el-dialog>
- template>
-
- <script>
- import { getDetailById} from "@/api/xxx/aaa"; //引入aaa.js中定义的getDetailById方法
- export default {
- name: "MyDialogComponent",
- data() {
- return {
- dialogTableVisible: false //弹出层是否打开
- };
- },
- methods: {
- open(id) { //打开弹出层方法被父组件 的 this.$refs.MyDialogComponent.open(row.id);方法调用
- this.dialogTableVisible = true;//打开弹出层
-
- getDetailById(id).then(response => { //调用引入方法查询详情记录
- this.form = response.data;
- });
- },
- }
- }
- script>
- <template>
-
- <MyDialogComponent ref="MyDialogComponent">MyDialogComponent>
- template>
-
- <script>
- import MyDialogComponent from '@/components/pcReleaseDialog/index' //引入详情弹出层组件,组件路径src/components/pcReleaseDialog/index.vue
-
- export default {
- components: { MyDialogComponent }, //注册弹出层组件
- data() {
- return {
- ...
- };
- },
- methods: {
- openDetail(row) { //点击列表标题显示详情弹出层
- this.$refs.MyDialogComponent.open(row.id); // 触发子组件的打开弹出层方法 ---必须手动触发,子组件定义弹出层打开的方法不能写在create方法里面,否则父组件刷新就会弹出
- //需要使用$refs.调用子组件的方法,那么在上面使用子组件标签时就必须定义 ref="MyDialogComponent",否则无法调用子组件的方法,如果只是组件间通信可以不需要定义 ref
- }
- }
- }
- script>
(如下代码,子组件在props中定义了pid,那么父组件只需要在data下的return中定义有这个属性,之后这个属性有任何值子组件都能同步到)
- <script>
- export default {
- props: { //定义了一个pid ,这个pid能接收到 父组件中通信的pid这个属性的值
- pid : {
- type: String,
- default: ''
- }
- },
- data() {
- ...
- },
- method{...}
- },