访问Vue3中另一个组件中的函数,使用组合API。

huangapple go评论60阅读模式
英文:

Access to a function in another component in Vue3 with composition API

问题

Sure, here's the translation of the provided content:

如何从子组件中访问父组件中的函数?
我想要在子组件中点击一个按钮,这个按钮应该调用父组件中的一个函数。

我有一个 Headline.vue 组件,其中有一个名为 LoadLogin 的 click() 方法:

<template>
  <div>
    <header>
      <h2>my-travel</h2>
      <div @click="navStatus" id="hamburger" class="hamburger-icon-container">
        <span class="hamburger-icon"></span>
      </div>
      <nav id="menue">
        <ul>
          <li @click="homeMenue"><a href="#home">Home</a></li>
          <li @click="loadLogin"><a href="#Login">Login</a></li>
          <li><a href="#about">about</a></li>
        </ul>
      </nav>
    </header>
  </div>  
</template>

<!-- ############################################################-->

<script setup>
const homeMenue = () => {
  console.log("Home")
}

const navStatus = () => {
  if (document.body.classList.contains('hamburger-active')) {
    document.body.classList.remove('hamburger-active');   
    document.getElementById("menue").classList.remove("closeMenue");
  } 
  else {
    console.log("open");
    document.getElementById("menue").classList.add("closeMenue");
    document.body.classList.add('hamburger-active');
  }
}
</script>

我的父组件 App.vue 有一个名为 loadLogin() 的函数:

<template> 
  <div>
    <HeadLine/>
    <hr/>      
    <div class="content">
      <LoginUser class="login"/>
      <ContentPicture class="contentPicture"/>
    </div>
  </div>
</template>

<script setup>
import HeadLine from '@/components/HeadLine.vue'
import ContentPicture from '@/components/ContentPicture.vue'
import LoginUser from '@/components/LoginUser.vue'

const loadLogin = () => {
    console.log("Login");
    const element = document.querySelector('.login');
    element.style.display = 'flex';
}
</script>

<style>
body, hr {
  margin: 0em;
}

.content {
  position: absolute;
  top: 60px;
  margin: 1em;
  z-index: -100;
}

.login {
  display: none;
}
</style>

This should provide the translated content you requested.

英文:

How can i access to a function in Parent Component from a chield component?
I want to click a button in chield component, which should call a function which is in the Parent component.

I have a Headline.vue component which has a click() methode calles LoadLogin:

&lt;template&gt;
  &lt;div&gt;
    &lt;header&gt;
      &lt;h2&gt;my-travel&lt;/h2&gt;
      &lt;div @click=&quot;navStatus&quot; id=&quot;hamburger&quot; class=&quot;hamburger-icon-container&quot;&gt;
        &lt;span class=&quot;hamburger-icon&quot;&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;nav id=&quot;menue&quot;&gt;
        &lt;ul&gt;
          &lt;li @click=&quot;homeMenue&quot; &gt;&lt;a href=&quot;#home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
          &lt;li @click=&quot;loadLogin&quot;&gt;&lt;a href=&quot;#Login&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#about&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/nav&gt;
    &lt;/header&gt;
  &lt;/div&gt;  
&lt;/template&gt;

&lt;!-- ############################################################--&gt;


&lt;script setup&gt;

const homeMenue = () =&gt; {
  console.log(&quot;Home&quot;)
}

const navStatus = () =&gt; {
  if (document.body.classList.contains(&#39;hamburger-active&#39;)) {
    document.body.classList.remove(&#39;hamburger-active&#39;);   
    document.getElementById(&quot;menue&quot;).classList.remove(&quot;closeMenue&quot;);
 } 
  else {
    console.log(&quot;open&quot;);
    document.getElementById(&quot;menue&quot;).classList.add(&quot;closeMenue&quot;);
    document.body.classList.add(&#39;hamburger-active&#39;);
 }
}
&lt;/script&gt;

My Parent Component App.vue has a function called loadLogin():

&lt;template&gt; 
  &lt;div&gt;
    &lt;HeadLine/&gt;
    &lt;hr/&gt;      
    &lt;div class=&quot;content&quot;&gt;
      &lt;LoginUser class=&quot;login&quot;/&gt;
    &lt;ContentPicture class=&quot;contentPicture&quot;/&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;

import HeadLine from &#39;@/components/HeadLine.vue&#39;
import ContentPicture from &#39;@/components/ContentPicture.vue&#39;
import LoginUser from &#39;@/components/LoginUser.vue&#39;

const loadLogin = () =&gt; {

    console.log(&quot;Login&quot;);
    const element = document.querySelector(&#39;.login&#39;);
    element.style.display = &#39;flex&#39;;
}

&lt;/script&gt;

&lt;style&gt;

body, hr {
  margin: 0em;
}

.content {
  position: absolute;
  top: 60px;
  margin: 1em;
  z-index: -100;
}

.login {
display: none
}

&lt;/style&gt;

答案1

得分: 1

这通常通过从子组件中触发事件并在父组件中监听该事件来处理。在Vue文档中有描述,可以参考这里

子组件:

&lt;li @click=&quot;$emit(&#39;login&#39;)&quot;&gt;&lt;a href=&quot;#Login&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;

父组件(我假设子组件是&lt;HeadLine /&gt;):

&lt;HeadLine @login=&quot;loadLogin&quot; /&gt;

另外,需要注意的是,使用document接口与Vue一起使用的情况非常少,我看到你正在大量使用它(querySelector、getElement、classList等)。我建议仔细阅读Vue文档,以充分利用框架来实现你想要的功能,因为Vue的文档非常好!

英文:

This is most commonly handled by emitting an event from the child and listening to it on the parent. It's described here in the Vue documentation.

Child component:

&lt;li @click=&quot;$emit(&#39;login&#39;)&quot;&gt;&lt;a href=&quot;#Login&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;

Parent component (I assume the child component is &lt;HeadLine /&gt;):

&lt;HeadLine @login=&quot;loadLogin&quot; /&gt;

On a side note, there are very few situations you need to use the document interface with Vue, which I see you're doing a lot of (querySelector, getElement, classList, etc.). I recommend thoroughly reading the Vue docs to learn how to take full advantage of the framework to do what you want, as the documentation is quite good!

huangapple
  • 本文由 发表于 2023年6月15日 08:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定