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

Vue-30、Vue非单文件组件。

非单文件组件:
一个组件包含n个组件

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>非单文件组件</title><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script type="text/javascript" src="./js/dayjs.min.js"></script><style></style>
</head>
<body>
<div id="root"><!--第三步:编写组件标签--><xuexiao></xuexiao><hr><!--第三步:编写组件标签--><xuesheng></xuesheng>
</div><script type="text/javascript">Vue.config.productionTip=false;//第一步创建school组件const school = Vue.extend({//el:'#root'   //一定不要写el 配置项,因为最终所有的组件都要被一个vm 管理,由vm决定服务于那个容器。template:`<div><h2>学校名称:{{schoolName}}</h2><h2>学校地址:{{address}}</h2><button @click="showName">点我提示学校名</button></div>`,data(){return{schoolName:'清华大学',address:'北京',}},methods:{showName(){alert(this.schoolName)}}});//第一步创建student组件const  student = Vue.extend({template:`<div><h2>学生姓名:{{studentName}}</h2><h2>学生年龄:{{age}}</h2></div>`,data(){return{studentName:'张仨',age:18,}}});//创建hello组件const  hello = vue.extend({template:`<div><h2>你好啊:{{name}}</h2></div>`,data(){return{name:'李四',}}});//全局注册Vue.component('hello',hello);new Vue({el:"#root",data:{msg:'消息'},//第二步:注册组件(局部注册)components:{xuexiao:school,xuesheng:student}})
</script>
</body>
</html>

总结:
在这里插入图片描述
注意点

在这里插入图片描述

组件的嵌套:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>组件嵌套</title><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script type="text/javascript" src="./js/dayjs.min.js"></script><style></style>
</head>
<body>
<div id="root"><app></app>
</div>
<script type="text/javascript">Vue.config.productionTip=false;//第一步student组件const  student = Vue.extend({template:`<div><h2>学生姓名:{{studentName}}</h2><h2>学生年龄:{{age}}</h2></div>`,data(){return{studentName:'张仨',age:18,}}});//第一步创建school组件const school = Vue.extend({//el:'#root'   //一定不要写el 配置项,因为最终所有的组件都要被一个vm 管理,由vm决定服务于那个容器。template:`<div><h2>学校名称:{{schoolName}}</h2><h2>学校地址:{{address}}</h2><hr><student></student></div>`,data(){return{schoolName:'清华大学',address:'北京',}},components:{student}});//第一步创建hello组件const hello = {name:'hello',template:`<div><h1>{{msg}}</h1></div>`,data(){return{msg:'欢迎你',}}};//定义app组件const app = Vue.extend({template:`<div><hello></hello><school></school></div>`,components:{hello,school}});new Vue({el:"#root",data:{msg:'消息'},components:{app}})
</script>
</body>
</html>
http://www.lryc.cn/news/284297.html

相关文章:

  • 7-6 实验2_1_判断两数的大小
  • POKT Network (POKT) :进军百亿美元市场规模的人工智能推理市场
  • 【STM32】STM32学习笔记-I2C通信外设(34)
  • 从数据角度分析年龄与NBA球员赛场表现的关系【数据分析项目分享】
  • 深入浅出Spring AOP
  • 火速收藏!2024 新年微信红包封面领取全攻略
  • 【RabbitMQ】RabbitMQ安装与使用详解以及Spring集成
  • 企业多云组网怎么办?
  • 背包问题(贪心) 二维01背包问题 Java
  • 2019年认证杯SPSSPRO杯数学建模D题(第二阶段)5G时代引发的道路规划革命全过程文档及程序
  • 可视化k8s页面(Kubepi)
  • 1434. 数池塘(四方向)-深度优先搜索-DFS
  • Mysql:重点且常用的操作和理论知识整理 ^_^
  • 小车辅助脚本编写
  • Modern C++ 一个例子学习条件变量
  • ora-12154无法解析指定的连接标识符
  • rust跟我学三:文件时间属性获得方法
  • 解决一个mysql的更新属性长度问题
  • [网络安全]DHCP 部署与安全
  • 自建ES集群
  • git rev-parse v406 ‘v4.0.4‘^{} master什么意思?
  • AI 编程的机会和未来:从 Copilot 到 Code Agent
  • git push --set-upstream origin master时超时失败的解决方案
  • beego的模块篇 - config自定义文件配置
  • YOLOv5-第Y2周:训练自己的数据集
  • 解决fxml图标无法显示
  • React Store及store持久化的使用
  • Hive添加第三方Jar包方式总结
  • Linux用户与文件的关系和文件掩码(umask)的作用
  • JS -- 正则表达式教程