Vue3组件化开发(一)

p11 组件

组件的拆分和嵌套

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

推荐安装的VS Cdoe插件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

组件的CSS作用域

在这里插入图片描述

组件的通信

在这里插入图片描述
在这里插入图片描述

父子组件的通信

在这里插入图片描述

父组件传递给子组件

在这里插入图片描述

在这里插入图片描述
父组件App.vue

<template>
  <show-message-vue title="哈哈哈哈" content="我是哈哈哈"></show-message-vue>
  <show-message-vue :title="title" :content="content"></show-message-vue>
  <show-message-vue v-bind="message"></show-message-vue>
</template>

<script>
import ShowMessageVue from "./ShowMessage.vue";
export default {
  components: {
    ShowMessageVue,
  },
  data() {
    return {
      title: "嘻嘻嘻",
      content: "我是嘻嘻嘻",
      message: {
        title: "嘿嘿嘿",
        content: "我是嘿嘿嘿",
      },
    };
  },
};
</script>

子组件ShowMessage.vue

<template>
  <h2>{{ title }}</h2>
  <p>{{ content }}</p>
</template>

<script>
export default {
  props: ["title", "content"],
};
</script>
props的对象用法

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

非prop的attribute

在这里插入图片描述

在这里插入图片描述

子组件传递给父组件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

子组件CounterOperation.vue

<template>
  <button @click="increment">+1</button>
  <button @click="decrement">-1</button>
  <input type="text" v-model.number="num" />
  <button @click="incrementNum">+n</button>
</template>

<script>
export default {
  emits: ["add", "sub", "addNum"],
  data() {
    return {
      num: 0,
    };
  },
  methods: {
    increment() {
      console.log("+1");
      this.$emit("add");
    },
    decrement() {
      console.log("-1");
      this.$emit("sub");
    },
    incrementNum() {
      this.$emit("addNum", this.num);
    },
  },
};
</script>

父组件App.vue

<template>
  <h2>{{ counter }}</h2>
  <counter-operation-vue
    @add="addOne"
    @sub="subOne"
    @addNum="addNum"
  ></counter-operation-vue>
</template>

<script>
import CounterOperationVue from "./CounterOperation.vue";
export default {
  components: {
    CounterOperationVue,
  },
  data() {
    return {
      counter: 0,
    };
  },
  methods: {
    addOne() {
      this.counter++;
    },
    subOne() {
      this.counter--;
    },
    addNum(num) {
      this.counter += num;
    },
  },
};
</script>

在这里插入图片描述

案例

在这里插入图片描述
父组件App.vue

<template>
  <TopControlVue :titles="titles" @titleClick="titleClick"></TopControlVue>
  <h2>{{ content[currentIndex] }}</h2>
</template>

<script>
import TopControlVue from "./TopControl.vue";
export default {
  components: {
    TopControlVue,
  },
  data() {
    return {
      titles: ["衣服", "鞋子", "裤子"],
      content: ["衣服页面", "鞋子页面", "裤子页面"],
      currentIndex: 0,
    };
  },
  methods: {
    titleClick(index) {
      this.currentIndex = index;
    },
  },
};
</script>

子组件TopControl.vue

<template>
  <div class="top-control">
    <div
      class="top-control-item"
      :class="{ active: currentIndex === index }"
      v-for="(item, index) in titles"
      :key="index"
      @click="itemClick(index)"
    >
      <span>{{ item }}</span>
    </div>
  </div>
</template>

<script>
export default {
  emits: ["titleClick"],
  props: {
    titles: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      currentIndex: 0,
    };
  },
  methods: {
    itemClick(index) {
      this.currentIndex = index;
      this.$emit("titleClick", index);
    },
  },
};
</script>

<style scoped>
.top-control {
  display: flex;
}
.top-control-item {
  flex: 1;
  text-align: center;
}

.top-control-item.active {
  color: red;
}

.top-control-item.active span {
  padding: 5px 10px;
  border-bottom: 3px solid red;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lalaxuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值