英文:
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:
<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>
My Parent Component App.vue has a function called 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>
答案1
得分: 1
这通常通过从子组件中触发事件并在父组件中监听该事件来处理。在Vue文档中有描述,可以参考这里。
子组件:
<li @click="$emit('login')"><a href="#Login">Login</a></li>
父组件(我假设子组件是<HeadLine />
):
<HeadLine @login="loadLogin" />
另外,需要注意的是,使用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:
<li @click="$emit('login')"><a href="#Login">Login</a></li>
Parent component (I assume the child component is <HeadLine />
):
<HeadLine @login="loadLogin" />
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论