【Vue】通过插槽slot实现【父组件·数据】操作【子组件】(图文+代码示例)

1、App.vue代码

<template>
  <div id="myapp">
    <!-- 第1行 -->
    <MenuRow>
      <template slot="left">
        <span class="menu_1"> {
   
   { menuName_1[0] }} </span>
      </template>
      <template slot="center">
        <span class="menu_1"> {
   
   { menuName_1[1] }}</span>
      </template>
      <template slot="right">
        {
   
   { menuName_1[2] }}
      </template>
    </MenuRow>
    <!-- 第2行 -->
    <MenuRow>
      <template slot="left">
        <span> {
   
   { menuName_2[0] }} </span>
      </template>
      <template slot="center">
        <span> {
   
   { menuName_2[1] }}</span>
      </template>
      <template slot="right">
        {
   
   { menuName_2[2] }}
      </template>
    </MenuRow>
  </div>
</template>

<script>
// 引入组件

import MenuRow from "./components/MenuRow.vue";

// 注册组件
export default {
  name: "App",
  components: {
    MenuRow,
  },
  data() {
    return {
      menuName_1: ["★", "头像名称修改", ">"],
      menuName_2: ["①", "系统设置", ">"],
    };
  },
  methods: {
    getInfo(a, b) {
      this.appSchoolName = a;
      this.appAddress = b;
    },
  },
};
</script>

<style scoped>
body {
  margin: 0;
  padding: 0;
  background-color: rgb(147, 149, 149);
}
#myapp {
  background-color: cornflowerblue;
 
}
.menu_1 {
  color: red;
}
</style>

2、MenuRow.vue

<template>
  <div class="row">
    <div class="left">
      <slot name="left"> 默认left</slot>
      <slot name="center">默认center</slot>
    </div>

    <div class="right">
        <slot name="right"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyItem",
  data() {
    return {
      menuName: "菜单名称",
    };
  },
  methods: {
    m_click(m_id) {
      this.changChecked(m_id);
    },
    delete_click(m_id) {
      this.deleteChecked(m_id);
    },
  },
};
</script>

<style scoped>
body {
  margin: 0;
  padding: 0;
}
.row {
  height: 40px;
  width: 100%;
  border-bottom: 1px rgb(237, 235, 235) solid;
  /* margin-bottom: 5px; */
  display: flex;
  flex-direction: row;
  background-color: rgb(250, 244, 235);
}

.row div:nth-child(1) {
  width: 0;
  flex-grow: 8;
  /* background-color: blueviolet; */
  line-height: 40px;
  padding-left: 15px;
  /* margin-left: 0px;  */
  /* padding-right: -15px; */
}

.row div:nth-child(2) {
  width: 0;
  flex-grow: 1;
  text-align: center;
  /* background-color: aquamarine; */
  line-height: 35px;
  font-size: 22px;
}
</style>

猜你喜欢

转载自blog.csdn.net/dxnn520/article/details/124668627
今日推荐