当前位置: 首页 > news >正文

vue组件

文章目录

      • 1.vue组件
      • 2.非单文件组件
          • 2.1组件创建
          • 2.2祖册组件
          • 2.3使用组件
      • 3.组件的嵌套
          • 3.1 school组件嵌套student
          • 3.2 app组件嵌套school和hellozujain
          • 3.3 vm里面引入app组件
      • 4.VueCompent
      • 5.单文件组件

1.vue组件

组件是实现应用中功能的局部代码和资源的集合

在这里插入图片描述

2.非单文件组件

2.1组件创建

注意:template要用div大盒子包裹

 // 创建组件const school = Vue.extend({template: `<div><h1>{{schoolname}}</h1><h2>{{adress}}</h2></div>`,data() {return {schoolname: "新理工",adress: "阿克苏",};},});
2.2祖册组件
//   全局注册组件Vue.component("school", school);new Vue({el: "#app",// 引入组件(局部)components: {school: school,},});
2.3使用组件
  <div id="app"><school></school></div>

3.组件的嵌套

在vue中vm的组件有app管理,在app组件内部引入定义的school和student组件

3.1 school组件嵌套student
 // 创建组件const school = Vue.extend({template: `<div><h1>{{schoolname}}</h1><h2>{{adress}}</h2><student></student></div>`,data() {return {schoolname: "新理工",adress: "阿克苏",};},// 祖册组件components: {student,},});
3.2 app组件嵌套school和hellozujain
 //创建app管理vm的组件const app = Vue.extend({// 使用template: `<div><school></school><hello></hello></div>`,// 祖册组件components: {school,hello,},});
3.3 vm里面引入app组件
 new Vue({el: "#app",template: ` <app></app>`,// 引入组件components: {app,},});

在vm里面通过引入app组件,
其中app组件是整个页面里引入组件的管理者。

4.VueCompent

1.在组件配置中,data函数,methods函数等,他们的this均是vuecompent实例对象。
2.在new data的配置中,,data函数,methods函数等,他们的this均是vue的实例对象。
3.每次调用组件返回的都是一个新的VueCompent实例对象。

在这里插入图片描述

5.单文件组件

school组件

<template><div><h1 class="demo">{{schoolname}}</h1><h2>{{adress}}</h2></div>
</template><script>export default {name:'School',data() {return {schoolname: "新理工",adress: "阿克苏",};},}//   默认暴露//   export default school
</script><style lang="scss">.demo{background-color: green;}
</style>

App组件

<template> <div><School></School><Student></Student></div>
</template><script>
import School from './School.vue'
import Student from './Student.vue'
export default {components: { School },name:'App',comments:{School,Student}
}
</script><style></style>

main.js

import App from './App.Vue'new Vue({el:"#app",template:`<App></App>`,comments:{App}
})

index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><div id="app"></div><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script><script src="./main.js"></script></body>
</html>
http://www.lryc.cn/news/4810.html

相关文章:

  • 让mybatis-plus支持null字段全量更新
  • MASA Stack 1.0 发布会讲稿——生态篇
  • 华为OD机试 - 火星文计算2(JS)| 真题+思路++考点+代码
  • 从春节后央行的首批罚单,看金融反欺诈反洗钱的复杂性
  • 【Hello Linux】Linux工具介绍 (yum vim)
  • 多种充电模式_手持无线充气泵方案
  • 【网络基础】DNS是什么
  • 二叉树的性质与推导及常见习题整理
  • 亚马逊卖家测评补单的重要性和缺点
  • Java类和对象超详细整理,适合新手入门
  • MySQL:连explain的type类型都没搞清楚,怎敢说精通SQL优化?
  • 6.11 极分解
  • Spring、SpringMVC、Shiro、Maven
  • element-plus 使用笔记
  • 《蓝桥杯每日一题》 前缀和·Acwing 3956. 截断数组
  • 促进关键软件高层次人才培养:平凯星辰与华东师范大学签订联合博士培养合作协议
  • Java程序员的日常——经验贴
  • 电商API社区,商品数据,关键词搜索等
  • LEADTOOLS 22.0.6 UPDATE-Crack
  • 什么是OJ? 东方博宜题库部分题解
  • 企业工程项目管理系统源码的各模块及其功能点清单
  • 【电商开发手册】订单-下单
  • 数据结构 - 优先级队列(堆)
  • PDF内容提取器:ByteScout PDF Extractor SDK Crack
  • 字母板上的路径[提取公共代码,提高复用率]
  • c# winform错误大全
  • AI_News周刊:第一期
  • 搭建mysql主从复制
  • 内存溢出、内存泄露的概述及常见情形
  • Linux 中断实验