laravel - 如何在vue3中监听父组件中的emit事件

标签 laravel vue.js vuejs3 vue-router vue-router4

我想将事件从子评论传递给父评论。 我在 vue2 中做了同样的事情,但我不知道如何在 vue3 中做到这一点。

这是子组件设置方法。

      setup(props, { emit }) {
            const router = useRouter();
            const form = ref(
                {
                    email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="50313a312910373d31393c7e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>",
                    password: "123456789",
                    isLoading: false,
                },
            );
            const user = ref("");
            const error = ref("");

            function login() {
                User.login(this.form).then(() => {
                    emit('login', true);
                   // this.$root.$emit("login", true);  -- vue2

                   localStorage.setItem("auth", "true");
                   router.push('/dashboard');
                })
                .catch(error => {});
            }

            return { form, login, user, error};
       }

从这里发出登录方法,我想听家长评论。

这是父组件,emit.on 方法在这里不起作用

  setup(props, { emit }) {
        const router = useRouter();
        const state = reactive({
            isLoggedIn: false,
        });

        onMounted(async () => {
            emit.on("login", () => {   // `vue2` this.$root.$on("login"`
                this.isLoggedIn = true;
            });
          
        });

最佳答案

在父组件中,您应该为该发出的事件添加一个处理程序:

<child @login="onLogin"></child>
 setup(props, { emit }) {
        const router = useRouter();
        const state = reactive({
            isLoggedIn: false,
        });
 
   function onLogin(){
      state.isLoggedIn=true,
    }

   return{state,onLogin}

} 

或者在单独的文件中创建一个名为 useAuth 的可组合函数:

import {reactive} from 'vue'

   const state = reactive({
            isLoggedIn: false,
        });
const useAuth=()=>{
  
   function onLogin(){
         state.isLogged=true;
    }
return {state,onLogin}
}

export default useAuth();

然后在两个组件中导入函数:

child :

import useAuth from './useAuth'
....
      setup(props, { emit }) {
            const router = useRouter();
             const {useAuth} =useAuth();
         ....
      function login() {
                User.login(this.form).then(() => {
                    onLogin() //will call the nested function that set loggedIn to true
                

                   localStorage.setItem("auth", "true");
                   router.push('/dashboard');
                })
                .catch(error => {});
            }

在父级中:

import useAuth from './useAuth'
....
  setup(props, { emit }) {
        const router = useRouter();
       const {state} =useAuth();
       //it replaces your local state 

关于laravel - 如何在vue3中监听父组件中的emit事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65434526/

相关文章:

php - Laravel 仅从数据库中抛出一组数据

laravel - Lumen Artisan 命令在测试时抛出 "Error: Call to a member function assertExitCode() on int"

javascript - 如何在vuejs中以编程方式动态创建组件并返回值

html - Vue-select 下拉菜单无法正常工作

laravel - 如何在 Laravel Nova 中过滤使用 BelongsTo 字段的选择列表?

mysql - Laravel:为当前用户获取最多购买的产品

javascript - VueJs - 双向绑定(bind)父子数据

css - 如何在 Vue.js 3 中动态绑定(bind)样式属性?

typescript - 带有 Typescript 注入(inject)的 Vue 3 无法按预期工作。传播类型只能从对象类型创建

javascript - Vue 3,在变量更改时调用 $emit