vue子传父、父传子 点击弹框获取数据

vue子传父、父传子 点击弹框获取数据

vue子传父 使用$emit传值

子组件:

<template>
	<div>
		<div slot="footer" class="dialog-footer">
		      <el-button @click="dialogFormVisible = false">取 消</el-button>
		      <el-button type="primary" @click="dialogConfirm">确 定</el-button>
		</div>
	</div>
</template> 
   
<script>
	export default {
	  name: "child",
	  data() {
	    return {
	    };
	  },
	methods:{
	    //子组件通过点击事件执行emit方法,将值传递给父组件
		dialogConfirm: function () {
	        //emit方法接收两个参数,第一个为自定义事件,第二个为传递的值
	        this.$emit("confirmRoom", row);
	    },
	}
}
</script>

父组件:

<template>
	<div>
		<el-form-item label="room" prop="meeting_room">
			<!-- 点击input框 -->
			<el-input @click.native="showRoom()" v-model="dataForm.meeting_room" auto-complete="off" clearable  maxlength="255" show-word-limit  placeholder="请选择" readonly>
			</el-input>
		</el-form-item>

		<!-- 弹出框 -->
		<el-dialog append-to-body title="标题" :visible.sync="dialogFormVisible">
	      <!-- 使用组件
	      父组件内通过自定义事件接收子组件传来的值并赋值   $emit相当于一个中转站
	      ref 被用来给DOM元素或子组件注册引用信息
	       -->
	      <child
	        @confirmRoom="changeRoom"
	        :isselect="isSelect2"
	        :isday="isday2"
	        ref="meetingroomselect"/>
	    </el-dialog>
    </div>
</template>  

<script>
//使用import 引入
import child from "@/views/child";
export default {
  name: "parent",
  //components中注册
  components: {
    child,
  },
  methods:{
		changeRoom(data) {
	      console.log(data, 1234);
	      console.log(data.room_name);
	      this.dataForm.meeting_room = data.room_name;
	      this.dataForm.meeting_room_id = data.room_id;
	      this.dataForm.address = data.address;
	      this.dialogFormVisible = false;
    	},
	}
}
</script>

父传子 子组件通过props接收参数

父组件:

<template>
  <div>
      <child
        @confirmRoom="changeRoom"
        :isselect="isSelect2"
        ref="meetingroomselect"
      />
  </div>
</template>

<script>
	import child from "@/views/child ";
	export default {
	  name: "parent",
	  components: {
	    child
	  },
	  data(){
		  return {
		      isSelect2: false
	      }
	  }
	}
</script>

子组件:

<template>
  <div>
    <el-table
    	highlight-current-row
    	v-if="isselect"
    	border
    	:data="tableData"
    	tooltip-effect="dark">
      <el-table-column
      	prop="room_name"
      	label="名称"
      	show-overflow-tooltip>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  export default {
    // 接收父组件的值
     props: {
	    // type 指定数据类型 default 默认值
	    isselect: {
	      // 标题栏显示与隐藏 只接收布尔值
	      type: Boolean,
	      // 如果值为空,默认为 false
	      default: false,
	    },
	  },
  }
</script>
要在 Vue 中实现点击点位弹出自定义弹框,可以按照以下步骤操作: 1. 在 Vue 组件中引入所需库: ```javascript import 'ol/ol.css'; import Overlay from 'ol/Overlay'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { Icon, Style } from 'ol/style'; import { fromLonLat } from 'ol/proj'; ``` 2. 在 Vue 组件的 `mounted` 钩子中创建地图、图层、数据源、样式和弹框: ```javascript mounted() { // 创建地图 this.map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }), this.vectorLayer ], view: new View({ center: fromLonLat([116.38, 39.9]), zoom: 10 }) }); // 创建矢量图层和数据源 this.vectorSource = new VectorSource(); this.vectorLayer = new VectorLayer({ source: this.vectorSource, style: new Style({ image: new Icon({ src: require('@/assets/marker.png'), anchor: [0.5, 1] }) }) }); // 创建弹框 this.popup = new Overlay({ element: document.getElementById('popup'), positioning: 'bottom-center', stopEvent: false, offset: [0, -50] }); this.map.addOverlay(this.popup); }, ``` 3. 在 Vue 组件的模板中创建地图容器和弹框容器: ```html <template> <div> <div id="map"></div> <div id="popup" class="ol-popup"> <a href="#" id="popup-closer" class="ol-popup-closer"></a> <div id="popup-content"></div> </div> </div> </template> ``` 4. 监听地图的 `singleclick` 事件,根据点击位置获取点位信息,然后在弹框中显示: ```javascript this.map.on('singleclick', (event) => { this.map.forEachFeatureAtPixel(event.pixel, (feature) => { const coordinates = feature.getGeometry().getCoordinates(); const content = `<p>${feature.get('name')}</p>`; document.getElementById('popup-content').innerHTML = content; this.popup.setPosition(coordinates); return true; }); }); ``` 5. 在弹框容器中添加关闭按钮的点击事件: ```javascript document.getElementById('popup-closer').addEventListener('click', () => { this.popup.setPosition(undefined); }); ``` 6. 根据需要自定义弹框的样式和内容。 注意:在使用自定义弹框时,需要注意弹框的位置和内容,避免重叠和遮挡。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值