vue 小白不懂错哪,求解说

App.vue

  <div class="hello" @click="ff">
    <demo :first="firstMsg" :data-second="secondMsg"></demo>
    <demo :first="firstMsg" :data-second="secondMsg"></demo>
    <demo :first="firstMsg" :data-second="secondMsg"></demo>
    <demo :first="firstMsg" :data-second="secondMsg"></demo>
  </div>
</template>

<script>
import Demo from "./components/Demo.vue";
export default {
  name: "hello",
  components: {
    Demo
  },
  data() {
    return {
      firstMsg: "first props",
      secondMsg: "secondMsg"
    };
  },
  methods: {
    ff(e) {
      if (e.target.dataset.second == "secondMsg") {
        console.log("通过事件委托拿到了自定义属性");
      }
    }
  }
};
</script>

Demo.vue

<template>
  <div>
    <div>{{first}}</div>
    <child v-bind="$attrs"></child>
  </div>
</template>
<script>
import child from "./nextChild.vue";
export default {
  name: "demo",
  props: ["first"],
  inheritAttrs: false,
  created() {
    console.log(this.$attrs);
  },
  components: {
    child
  }
};
</script>

nextChild.vue

<template>
  <div>{{data-second}}</div>
</template>

<script>
export default {
  name: "child",
  props: ["data-second"],
  created() {
    console.log("孙子组件", this.$attrs);
  }
};
</script>
阅读 1.3k
1 个回答

变量名怎么能有中划线呢,程序区分不了他是不是减号呀。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题