vue+elementUI中Dialog实现组件弹框以及子父组件页面传值

弹框

使用element-UI中的Dialog 对话框 + vue组件结合实现~~~·~

定义html

<div @click="MyAnalyze()">我的区划</div>

 <el-dialog title="" :visible.sync="dialogBiomeVisible">
      <NationalBiome :closeValue="'TypeBiome'" @closeMoule="closeMoule"></NationalBiome>
 </el-dialog>

 

<el-dialog> </el-dialog> 这是element-UI自带的标签, 

:visible.sync="" 是用来控制显示或者隐藏状态,当dialogBiomeVisible的值为true的时候为显示,false的时候就是隐藏,

<NationalBiome></NationalBiome> 这个就是自己定义的组件名;

引入组件

<script>
import NationalBiome from './National_Biome';  //组件内部引入 ---编辑生态区
export default {
  name: 'App',
  components:{  //定义组件
    NationalBiome, //编辑生态区
  },
}

components中用来定义组件名称,名称要和important的时候保持一致

 

传值--父组件传子组件

父组件中的定义

父组件向之组件传值的时候,需要在组件标签中定义一个自定义属性进行传值,可以传一个字符串,也可以传一个变量

子组件中的定义(取值)

子组件在export default{}中通过props来进行接收,使用的时候可以直接当变量使用,js中通过this.closeValue取值,html中{{closeValue}}

传值--子组件传父组件

子组件中的定义

子组件中通过触发某个事件,然后通过this.$emit()来进行传值,第一个值是事件名,第二个是传递的值

<!--关闭按钮-->
<div class="fr right_icon" @click="closeBtn()">
     <s></s>
</div>
 methods:{
     //点击关闭的时候--给调用的父组件传值
     closeBtn(){
          if(this.closeValue == "TypeBiome"){
              this.$emit('closeMoule','closeMoule1')
           }
      }
}

父组件定义(取值)

定义一个事件监听子组件的数据变化  closeMoule方法的参数就是子组件中closeMoule1的值

<SpeciesAnalyzed :closeValue="'TypeBiome'" @closeMoule="closeMoule"></SpeciesAnalyzed>
methods:{
//获取自组建传过来的值--关闭编辑生态区的弹框
    closeMoule(e){
        alert(e)
    }
}

 

  • 17
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
1. 父组件向子组件传值: 在父组件使用子组件时,可以通过在子组件标签使用属性绑定的方式将数据传递给子组件。例如: ```html <template> <div> <child-component :prop-name="parentData"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentData: '这是来自父组件的数据' }; } }; </script> ``` 在子组件,可以通过 `props` 来接收父组件传递过来的数据。例如: ```html <template> <div>{{ propName }}</div> </template> <script> export default { props: { propName: { type: String, required: true } } }; </script> ``` 2. 子组件向父组件传值: 在子组件,可以通过 `$emit` 方法触发自定义事件,并将数据传递给父组件。例如: ```html <template> <div> <button @click="sendDataToParent">向父组件传递数据</button> </div> </template> <script> export default { methods: { sendDataToParent() { this.$emit('custom-event', '这是来自子组件的数据'); } } }; </script> ``` 在父组件,可以通过在子组件标签监听自定义事件的方式接收子组件传递过来的数据。例如: ```html <template> <div> <child-component @custom-event="receiveDataFromChild"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { receiveDataFromChild(data) { console.log(data); // 输出:这是来自子组件的数据 } } }; </script> ``` 3. 父子组件互相传值: 可以将以上两种方式结合起来,实现父子组件互相传值。例如: ```html <template> <div> <child-component :prop-name="parentData" @custom-event="receiveDataFromChild"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentData: '这是来自父组件的数据' }; }, methods: { receiveDataFromChild(data) { console.log(data); // 输出:这是来自子组件的数据 } } }; </script> ``` 子组件: ```html <template> <div> <button @click="sendDataToParent">向父组件传递数据</button> </div> </template> <script> export default { props: { propName: { type: String, required: true } }, methods: { sendDataToParent() { this.$emit('custom-event', '这是来自子组件的数据'); } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值