前端页面全新的写法(第七课)Vue中的组件

简介: 前端页面全新的写法(第七课)Vue中的组件

VueCli框架的实操内容(第七课)Vue中的组件


组件是可复用的 Vue 实例, 把一些公共的模块抽取出来,然后写成单独的的工具组件或者页面,在需要的页面中就直接引入即可那么我们可以将其抽出为一个组件进行复用。例如 页面头部、侧边、内容区,尾部,上传图片,等多个页面要用到一样的就可以做成组件,提高了代码的复用率。

个人观点:Vue组件等价于模块思想

文档内容解释

 

(6条消息) VueCLi的安装步骤(第四课)_星辰镜的博客-CSDN博客

VueCli项目的创建结构图

 

控制器操作自己的页面信息

组件注册 | Vue.js (vuejs.org)

前面我们是将所有的逻辑放到一个App.vue中:


在之前的案例中,我们只是创建了一个组件App;


如果我们一个应用程序将所有的逻辑都放在一个组件中,那么这个组件就会变成非常的臃 肿和难以维护;


所以组件化的核心思想应该是对组件进行拆分,拆分成一个个小的组件;


再将这些组件组合嵌套在一起,最终形成我们的应用程序;


我们来分析一下下面代码的嵌套逻辑,假如我们将所有的代码逻辑都放到一个App.vue组件 中:


我们会发现,将所有的代码逻辑全部放到一个组件中,代码是非常的臃肿和难以维护的。


并且在真实开发中,我们会有更多的内容和代码逻辑,对于扩展性和可维护性来说都是非 常差的。


所以,在真实的开发中,我们会对组件进行拆分,拆分成一个个功能的小组件。

 

 

 

 

组件又是上面三张图的意思 将整个页面分开实现功能 在用组件思想 组合在一起 便于后期期维护


App.vue

  <div class="app">
    <!--!主模块的内容信息 -->
    <h1>主版块的内容信息</h1>
    <!-- 头部 -->
    <header-item  attribute="接收子组件转来的信息" name="张三" age="20" height="111"></header-item>
    <header-item  attribute="接收子组件转来的信息" name="李四" age="ASDF" height="111"></header-item>
    <header-item  attribute="接收子组件转来的信息" name="王五" age="202" height="111"></header-item>
    <header-item attribute="接收子组件转来的信息"  name="张璐" age="203" height="111"></header-item>
    <!-- 中部 -->
    <banner :info="obj"></banner>
    <!-- 脚步 -->
   <footers class="product" name="我叫张上偶" age="20" id="1001002"></footers>
   <footers :class="$attrs.class " name="我叫张上偶" age="20" id="1001002"></footers>
   <footers class="product" name="我是谁" age="20" id="1001002"></footers>
   <footers class="product" name="我来自哪里" age="20" id="1001002"></footers>
   <mysolt></mysolt>
   <four></four>
   <div></div>
   <vue-sum-egg-five></vue-sum-egg-five>
  </div>
<template>
  <div class="app">
    <!--!主模块的内容信息 -->
    <h1>主版块的内容信息</h1>
    <!-- 头部 -->
    <header-item  attribute="接收子组件转来的信息" name="张三" age="20" height="111"></header-item>
    <header-item  attribute="接收子组件转来的信息" name="李四" age="ASDF" height="111"></header-item>
    <header-item  attribute="接收子组件转来的信息" name="王五" age="202" height="111"></header-item>
    <header-item attribute="接收子组件转来的信息"  name="张璐" age="203" height="111"></header-item>
    <!-- 中部 -->
    <banner :info="obj"></banner>
    <!-- 脚步 -->
   <footers class="product" name="我叫张上偶" age="20" id="1001002"></footers>
   <footers :class="$attrs.class " name="我叫张上偶" age="20" id="1001002"></footers>
   <footers class="product" name="我是谁" age="20" id="1001002"></footers>
   <footers class="product" name="我来自哪里" age="20" id="1001002"></footers>
   <mysolt></mysolt>
   <four></four>
   <div></div>
   <vue-sum-egg-five></vue-sum-egg-five>
  </div>
</template>
<script>
import banner from "../src/components/banner.vue";
import footers from '../src/components/footer.vue';
import mysolt from '../src/components/mySoltOne.vue'
import four from '../src/components/four.vue'
import VueSumEggFive from "../src/components/V-X.vue"
// import footer_item from "./footers.vue"
export default { 
  // eslint-disa le-next-line vue/multi-word-component-names
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
      // 在父模块中定义对象
      obj:{
        id:"1001001",
        name:"我是李四上",
        age:"78",
        height:"178",
        weight:"78",
        color:"red",
        sex:'男'
      }
    };
  },
//   局部注册
  components: {
    banner,
    footers,
    mysolt,
    four,
    // eslint-disable-next-line vue/no-unused-components
    VueSumEggFive
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>
<style scoped>
*{
    /* background-color: rgb(0, 100, 0); */
    background-color: rgb(151, 219, 178);
    border-top: 4px solid rgb(244, 8, 86) ;
}
h2 {
  background-color: azure;
}
h1 {
  color: rgb(20, 87, 116);
  background-color: rgb(159, 248, 251);
  text-align: center;
}
h3 {
  background-color: antiquewhite;
  color: cyan;
}
</style>

One.vue

import banner from "../src/components/banner.vue";
import footers from '../src/components/footer.vue';
import mysolt from '../src/components/mySoltOne.vue'
import four from '../src/components/four.vue'
import VueSumEggFive from "../src/components/V-X.vue"
// import footer_item from "./footers.vue"
export default { 
  // eslint-disa le-next-line vue/multi-word-component-names
  name: "app",
  data() {
    return {
      message: "Hello world vue cli",
      // 在父模块中定义对象
      obj:{
        id:"1001001",
        name:"我是李四上",
        age:"78",
        height:"178",
        weight:"78",
        color:"red",
        sex:'男'
      }
    };
  },
//   局部注册
  components: {
    banner,
    footers,
    mysolt,
    four,
    // eslint-disable-next-line vue/no-unused-components
    VueSumEggFive
  }
<template>
  <div class="header">
    <div>
      <h1>我是第一模块</h1>
      <h1>我是头部提示信息{{ age }}</h1>
      <h1>{{ message }}{{ name }}___{{ age }}___{{ height }}</h1>
      <h2>我是头部信息</h2>
      <h3>
        <nav><span>11111</span>&nbsp;&nbsp;<span>22222</span></nav>
      </h3>
    </div>
  </div>
</template>
<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "header",
  // 
  props: {
    age: {
      type: Number,
      required: true,
      default: 18,
    },
  },
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {},
};
</script>
<style scoped>
* {
  font-size: 23px;
  background-color: rgb(17, 96, 123);
  color: white;
}
</style>

Banner.vue

<template>
  <div class="banner">
    <div>
      <h1>Two</h1>
      <h2>我是身体的一部分</h2>
      <ul>
        <!-- 用户转入的对象信息 -->
        <!-- 子模块 -->
        <h4>{{info}}</h4>
        <li>&nbsp;&nbsp;我是学生&nbsp;&nbsp;{{info.id}}</li>
        <li>&nbsp;&nbsp;我是学生&nbsp;&nbsp;{{info.name}}</li>
        <li>&nbsp;&nbsp;我是学生&nbsp;&nbsp;{{info.sex}}</li>
        <li>&nbsp;&nbsp;我是学生&nbsp;&nbsp;{{info.height}}cm</li>
        <li>&nbsp;&nbsp;我是学生&nbsp;&nbsp;{{info.weight}}kg</li>
      </ul>
      <product class="product"></product>
      <h1>{{count}}</h1>
      <product @sonCom="getSon"  class="product"></product>
    </div>
  </div>
</template>
<script>
import Product from './product.vue';
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "banner",
  props:["info"],
  emits:{
    getSon:function(e){
      return e
    }
  },
  components:{
    Product 
  },
  data() {
    return {
      message: "Hello world vue cli",
      count:0
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
    getSon(e){
      if(e){
        this.count--
      }else{
        this.count++
      }
      // console.log(e)
    }
  },
};
</script>
<style scoped>
*{
  font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
  font-size: 20px;
  background-color: rgb(4, 117, 77);
  color: rgb(255, 255, 255);
}
</style>

产品组件

<template>
    <div>
    <h1 >学生信息表格</h1>
    <table @click="emits">
      <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>爱好</th>
      </tr>
      <tr>
        <td>1001</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10012</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10012</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10014</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10015</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10016</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10017</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10018</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10019</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
      <tr>
        <td>10010</td>
        <td>李四光</td>
        <td>女</td>
        <td>34</td>
        <td>王者荣耀</td>
      </tr>
    </table>
    <button @click="emitse(false)"> -1</button>
    <button @click="emitse(true)"> +1</button>
  </div>
</template>
<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "product",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("我是第一条的数据信息内容");
    },
    // 字组件专递个父组件的对象
    emits(flag){
      this.$emit("sonCom",flag)
      // this.$emit("sonCom", 监听事件
      // this.$emit("sonCom",{
        // name:"我叫对象",
        // age:123,
        // height:190,
        // weight:78,
        // sex:"男",
      // })
    }
  },
};
</script>
<style scoped>
*{
    /* background-color: rgb(0, 100, 0); */
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    font-size:20px;
    border: 2px solid rgb(125, 193, 232);
    border-bottom: 1px solid rgb(255, 0, 0);
    margin: 2px;
    background-color: rgb(10, 79, 101);
}
table {
  width: 100%;
  border-collapse: collapse;
  margin: auto;
  border: 2px solid rgb(0, 0, 0);
  text-align: center;
}
th td {
  border: 1px solid rgb(255, 0, 0);
}
td{
  background-color: lightblue;
    border-left: 3px solid rgb(255, 166, 0);
   border-bottom: 2px solid rgb(9, 74, 97); 
}
h1{
  text-align: center;
}
</style>

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "product",
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("我是第一条的数据信息内容");
    },
    // 字组件专递个父组件的对象
    emits(flag){
      this.$emit("sonCom",flag)
      // this.$emit("sonCom", 监听事件
      // this.$emit("sonCom",{
        // name:"我叫对象",
        // age:123,
        // height:190,
        // weight:78,
        // sex:"男",
      // })
    }
  }

footer.vue

<template>
  <div class="footers">
    <div>
      <!--  字模块-->
      <h1>{{attribute}}__{{name}}_{{age}}_{{id}}</h1>
      <h2>我是脚本信息</h2>
      <h3>信息脚本</h3>
      <h2>我是动态的脚本信息内容</h2>
    </div>
  </div>
</template>
<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "footers",
  props:["attribute","name","age","id"],
  data() {
    return {
      message: "Hello world vue cli",
    };
  },
  methods: {
    btnclick() {
      console.log("为难忘");
    },
  },
};
</script>
<style scoped>
*{
    background-color: rgb(173, 227, 229);
    color: rgb(6, 6, 6);
    font-size: 20px;
}
</style>

four.vue

<template>
    <div class="four">
    <h1>我是文本的信息内容介绍four</h1>
    </div>
</template>
<script>
export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:'four',
    data(){
        return{
            message:"Hello world vue cli"
        }
    },
    methods:{
        btnclick(){
            console.log("为难忘")
        }
    }
}
</script>
<style scoped>
*{
    background-color: chartreuse;
}
</style>

mySoltOne.vue

<template>
    <div class="mysolt">
        <h1>开始</h1>
        <slot>
            <h1>我的内容展示</h1>
        </slot>
        <h1>结尾</h1>
    </div>
</template>
<script>
export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:'mysolt',
    data(){
        return{
        }
    },
    methods:{
    }
}
</script>
<style scoped>
div{
    border-top: 4px solid rgb(13, 71, 96);
    background-color: rgb(255, 140, 0);
    border-radius: 20px;
    font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
    font-size: 24px;
}
</style>

这个项目自己理解一下会对你有帮助 后期会有项目实战

相关文章
|
6月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(二):CSS伪类:UI伪类、结构化伪类;通过伪类获得子元素的第n个元素;创建一个伪元素展示在页面中;获得最后一个元素;处理聚焦元素的样式
伪类:伪类这个叫法源自于它们跟类相似,但实际上并没有类会附加到标记中的标签上。 伪类分为两种(以及新增的伪类选择器): UI伪类:会在HTML元素处于某种状态时(例如:鼠标指针位于连接上),为该元素应用CSS样式。 :hover 结构化伪类:会在标记中存在某种结构上的关系时 例如: 某元素是一组元素中的第一个或最后一个,为该元素应用CSS样式。 :not和:target(CSS3新增的两个特殊的伪类选择器)
652 2
|
7月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
619 2
|
10月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
1057 0
|
11月前
|
JavaScript 前端开发 Java
制造业ERP源码,工厂ERP管理系统,前端框架:Vue,后端框架:SpringBoot
这是一套基于SpringBoot+Vue技术栈开发的ERP企业管理系统,采用Java语言与vscode工具。系统涵盖采购/销售、出入库、生产、品质管理等功能,整合客户与供应商数据,支持在线协同和业务全流程管控。同时提供主数据管理、权限控制、工作流审批、报表自定义及打印、在线报表开发和自定义表单功能,助力企业实现高效自动化管理,并通过UniAPP实现移动端支持,满足多场景应用需求。
1025 1
|
12月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1339 4
|
10月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
前端开发 API 开发者
harmonyOS基础- 快速弄懂HarmonyOS ArkTs基础组件、布局容器(前端视角篇)
本文由黑臂麒麟(6年前端经验)撰写,介绍ArkTS开发中的常用基础组件与布局组件。基础组件包括Text、Image、Button等,支持样式设置如字体颜色、大小和加粗等,并可通过Resource资源引用统一管理样式。布局组件涵盖Column、Row、List、Grid和Tabs等,支持灵活的主轴与交叉轴对齐方式、分割线设置及滚动事件监听。同时,Tabs组件可实现自定义样式与页签切换功能。内容结合代码示例,适合初学者快速上手ArkTS开发。参考华为开发者联盟官网基础课程。
1145 75
harmonyOS基础- 快速弄懂HarmonyOS ArkTs基础组件、布局容器(前端视角篇)
|
12月前
|
移动开发 前端开发 JavaScript
Vue与React两大前端框架的主要差异点
以上就是Vue和React的主要差异点,希望对你有所帮助。在选择使用哪一个框架时,需要根据项目的具体需求和团队的技术栈来决定。
638 83
|
11月前
|
JavaScript 前端开发 编译器
Vue与TypeScript:如何实现更强大的前端开发
Vue.js 以其简洁的语法和灵活的架构在前端开发中广受欢迎,而 TypeScript 作为一种静态类型语言,为 JavaScript 提供了强大的类型系统和编译时检查。将 Vue.js 与 TypeScript 结合使用,不仅可以提升代码的可维护性和可扩展性,还能减少运行时错误,提高开发效率。本文将介绍如何在 Vue.js 项目中使用 TypeScript,并通过一些代码示例展示其强大功能。
450 22
|
10月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件的实现代码:支持自定义表情库、快捷键发送和输入框联动的聊天表情解决方案
本文详细介绍了在 Vue 项目中实现一个功能完善、交互友好的表情包输入组件的方法,并提供了具体的应用实例。组件设计包含表情分类展示、响应式布局、与输入框的交互及样式定制等功能。通过核心技术实现,如将表情插入输入框光标位置和点击外部关闭选择器,确保用户体验流畅。同时探讨了性能优化策略,如懒加载和虚拟滚动,以及扩展性方案,如自定义主题和国际化支持。最终,展示了如何在聊天界面中集成该组件,为用户提供丰富的表情输入体验。
740 8

热门文章

最新文章