我最近开始使用VueJS,我正在使用v3,似乎在调用父对象上的方法时遇到了问题.子对象中的emit函数似乎没有发出事件,父对象中没有接收到任何内容.

我已经包括了父母和子元素来展示我是如何设置的

Parent

<template>
  <First/>
  < Child v-bind:sample="sample" @enlarge-text="onEnlargeText"/>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import axios from 'axios';
import First from './First.vue';
import Child from './Child.vue';

export default defineComponent({
  name: 'Container',
  components: {
    First,
    Child,
  },
  methods: {
    onEnlargeText() {
      console.log('enlargeText');
    },
  },
  data: () => ({
    sample: [],
    parentmessage: '',
  }),
  created() {
    axios.get('http://localhost:8080/getData')
      .then((response) => {
        console.log(response);
        this.sample = response.data;
      })
      .catch((error) => {
        console.log(error);
      });
  },
});
</script>

Child

<template>
  <div id="add">
    <form id="signup-form" @submit.prevent="submit">
      <label for="text">Text:</label>
      <input type="text" v-model="text" required>
      <p class="error" >{{ error }}</p>
      <div class="field has-text-right">
        <button type="submit" class="button is-danger">Submit</button>
      </div>
    </form>
    <button v-on:click="tryThis">
      Enlarge text
    </button>
</div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import axios from 'axios';

interface SampleInterface {
  text: string;
  error: string;
}

export default defineComponent({
  name: 'Add',
  data: (): AddInterface => ({
    text: '',
    error: '',
  }),
  methods: {
    tryThis() {
      this.$emit('enlarge-text');
    },
    submit() {
      this.$emit('enlarge-text');
    },
  },
});
</script>

这应该怎么做?我错过了什么吗?

我在想我还能用$emit吗?

推荐答案

您应该添加包含已发出事件名称的新emits option:

子元素:

<template>
  <div id="child">
    <button v-on:click="tryThis">Enlarge text</button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Child",
  emits: ["enlargeText"],
  methods: {
    tryThis() {
      console.log("trying");
      this.$emit("enlargeText", "someValue");
    },
  },
});
</script>

起源:

<template>
  <div>
    <p>Container</p>
    <Child @enlargeText="onEnlargeText" />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import Child from "./Child.vue";

export default defineComponent({
  name: "UrlContainer",
  components: {
    Child,
  },
  methods: {
    onEnlargeText() {
      console.log("enlarging text");
    },
  },
});
</script>

100

Vue.js相关问答推荐

vue3中的转换可以在滑入但不能滑出时工作

Vutify-v-工具提示-不使用键盘轻击按钮显示

从组件中检索 Shopware CMS 编辑器中的自定义实体数据

Vue 3组合式API如何测试是否发出正确的事件

Vuetify 3 - NavBar 组件占据整个视口高度

在计算(computed)属性上调用数组方法

如何将 axios/axios 拦截器全局附加到 Nuxt?

VueJS:向左,元素的顶部位置?

为什么Vue.js 使用单字标签

如何在 Vue 3 中使用 SSR

如何告诉 Vue 应用使用 Firebase 模拟器?

如何在 Vue 中转发 $refs

如何在 Vuex 中获取 Vue 路由参数?

npm run dev 和 npm run production 的区别

在Vue中单击路由链接上的激活方法

如果我刷新我的网页,Vuex store是空的

在 Vue JS 组件中使用 Laravel 路由的最佳方法是什么

使用 Vue 的点击编辑文本字段

如何在 Promise 回调中更新 Vue 应用程序或组件的属性?

用于 nuxt.js 构建的自定义 index.html