Home  >  Article  >  Web Front-end  >  How to implement menu drop-down box in vue3 through render function

How to implement menu drop-down box in vue3 through render function

PHPz
PHPzforward
2023-05-10 16:28:211655browse

Technical Solution

First write a drop-down box component

First, we first write a component to display the content of the drop-down box. The component name starts with : Select.vue

<template>
  <div class="select-wrap">
    <span>福利商城</span>
    <span>Saas平台</span>
    <span>活动定制</span>
  </div>
</template>

How to implement menu drop-down box in vue3 through render function

Rendering component

We want to render this component on the web page, the operation should be It's like this:

When the mouse moves to the product service, the drop-down box component is rendered as a component instance at the appropriate position on the page.

In vue3, rendering a Vonde, the core logic is as follows:

import { createVNode, h, render, VNode } from &#39;vue&#39;
import component from "./component.vue"
//1、创造包裹虚拟节点的div元素
const container = document.createElement(&#39;div&#39;);
//2、创造虚拟节点
vm = createVNode(component)
//3、将虚拟节点创造成真实DOM
render (vm, container)
//4、将渲染的结果放到body下
document.body.appendChild(container.firstElementChild)

To know the location of component rendering, we must know the parent component (that is, the dom location of the product service), we use ref to Get the dom information of the parent component.

// App.vue
<div ref="select">
  <span class="name">产品服务</span> 
</div>
<script setup >
  import { ref } from "vue"
  const select = ref()
</script>

When the mouse moves over the product service element, the drop-down box is rendered. We add a function

// App.vue
<div ref="select">
  <span class="name">产品服务</span> 
</div>
<script setup >
import { ref } from "vue"
import Select from "./Select.vue"
const select = ref()
function createDom(){
  //1、创造包裹虚拟节点的div元素
  const container = document.createElement(&#39;div&#39;);
  //2、创造虚拟节点
  let vm = createVNode(Select)
  //3、将虚拟节点创造成真实DOM
  render (vm, container)
  //4、将渲染的结果放到body下
  document.body.appendChild(container.firstElementChild) 
}
</script>

Then, add the position judgment

function createDom(){
  const left = select.value.offsetLeft + "px"
  const width = select.value.getBoundingClientRect().left + "px"
  const props = {
    width,
    left,
  }
  //1、创造包裹虚拟节点的div元素
  const container = document.createElement(&#39;div&#39;);
  //2、创造虚拟节点
  let vm = createVNode(Select,props)
  //3、将虚拟节点创造成真实DOM
  render (vm, container)
  //4、将渲染的结果放到body下
  document.body.appendChild(container.firstElementChild) 
}

Among them, prop is passed The distance parameter for the Select component can be set within the component.

Destroy the component

To destroy the component, we can use render to render an empty object

render (vm, container)

If we need a child component to destroy itself, we can use parent-child value passing

<template>
  <div class="select-wrap" @mouseleave="beforeUnload">
    <span>福利商城</span>
    <span>Saas平台</span>
    <span>活动定制</span>
  </div>
</template>
<script   setup>
const emit = defineEmits([&#39;destroy&#39;])
function beforeUnload(){
 emit(&#39;destroy&#39;)
}
</script>

In the parent component, we need to add an onDestroy function in props. Note: onDestroy is written in camel case

function createDom(){
  const left = select.value.offsetLeft + "px"
  const width = select.value.getBoundingClientRect().left + "px"
  const props = {
    width,
    left,
    onDestroy: () => {
      render(null, container)
    },
  }
  //1、创造包裹虚拟节点的div元素
  const container = document.createElement(&#39;div&#39;);
  //2、创造虚拟节点
  let vm = createVNode(Select,props)
  //3、将虚拟节点创造成真实DOM
  render (vm, container)
  //4、将渲染的结果放到body下
  document.body.appendChild(container.firstElementChild) 
}

The above is the detailed content of How to implement menu drop-down box in vue3 through render function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete