Vue组件

作者: kzc爱吃梨 | 来源:发表于2019-09-26 10:16 被阅读0次

写好的组件

<template>
  <div id="cell" v-on:click="a = true">
    <template v-if="a">{{text}}</template>
    <template v-else></template>
  </div>
</template>

<script>
  export default {
    data() {
      return {a: false, text: "O"};
    }
  }
</script>

<style>
#cell {
  color: #9a55a3;
  border: 1px solid black;
  height: 100px;
  width: 100px;
  font-size: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

引入组件

<template>
  <div>
    <div class="row">
      <Cell />
      <Cell />
      <Cell />
    </div>
    <div class="row">
      <Cell />
      <Cell />
      <Cell />
    </div>
    <div class="row">
      <Cell />
      <Cell />
      <Cell />
    </div>
  </div>
</template>

<script>
import Cell from "./Cell";
export default {
  components: { Cell }  //定义
};
</script>

<style>
.row  {
  display: flex;
}
</style>

他的好处是组件与组件是独立的不会相互干扰。两个Cell是用了相同代码的两个对象。

相关文章

网友评论

      本文标题:Vue组件

      本文链接:https://www.haomeiwen.com/subject/qlahuctx.html