Vue-组件(二)

摘要:
DOCTYPE html˃23456组件-注册、调用、道具789/script˃10111218192021˂!--Ps:组件名称由驼峰命名,在调用时也可以使用“在大写前添加并将大写改为小写”的编写方法。22例如,组件名称:ComtName Call:或23当直接在DOM中使用时,只有Com名称有效。24--˃252627//全局注册28Vue组件34//本地注册35varComponent_2={36template:'thisComponent_2'37}38//本地注册的组件在其子组件中不可用。如有必要,请按照以下步骤进行操作:39varComponent_3={40components:{41'component_2':component_242},43template:'44thisiscomponent_3Star4546thisiscomponent_3结束47'48}49/*使用babel和webpack:50*importComponent_3from'/组件_3.vue'51*导出默认{52*组件:{53*组件_354*},55*/…56*}57*/58varvm1=newVue66676875<组件实例的组件_ 4title=“Thisisprop4_1”˃76<组件_ 4title=“this isprop4_2”˃77<组件_ 4 title=”Thisisprop_4_3“˃78<!

组件的案例,可通过案例更好的学习组件

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6     <title>组件-注册、调用、prop</title>
  7     <script src="./jquery-3.5.1.min.js"></script>
  8     <script src="./vue.js"></script>
  9     <script src="./vue-router.js"></script>
 10 </head>
 11 <body>
 12     <!-- 调用、注册 -->
 13     <div>
 14         <div id="app1">
 15         <!-- 调用组件 -->
 16         <component_1></component_1>
 17         <!-- 组件可复用,并且数值为独立的-->
 18         <component_1></component_1>
 19         <component_2></component_2>
 20         <component_3></component_3>
 21         <!-- ps:组件名字使用驼峰命名,调用时使用‘大写前加-,大写改小写的’写法也是可以的
 22                 如:组件名:ComtName   调用:<ComName>或<Com-name> 
 23                 直接在DOM中使用时只有Com-name有效
 24         -->
 25         </div>
 26         <script>
 27             //全局注册
 28             Vue.component('component_1',{//component_1为组件名
 29                 data:function(){//data必须是函数的形式
 30                     return {count:0}
 31                 },
 32                 template:'<div><h2>this is component_1</h2><button @click="count++">The {{count}} .</button></div>'
 33             })
 34             //局部注册
 35             var Component_2 = {
 36                 template:'<h2>this is Component_2</h2>'
 37             }
 38             //局部注册的组件在其子组件中不可用,如需要则如下操作
 39             var Component_3 = {
 40                 components:{
 41                     'component_2':Component_2
 42                 },
 43                 template:'<div>
 44                           <h2>this is component_3 Star</h2>
 45                           <component_2></component_2>
 46                           <h2>this is component_3 End</h2>
 47                           </div>'
 48             }
 49             /* 使用babel和webpack:
 50              * import Component_3 from './Component_3.vue'
 51              * export default{
 52              *     components:{
 53              *         Component_3
 54              *     },
 55              *     //...
 56              * }
 57              */
 58             var vm1 = new Vue({
 59                 el:'#app1',
 60                 //局部注册的组件在其子组件中不可用(33行)
 61                 components:{
 62                     'component_2':Component_2,
 63                     'component_3':Component_3
 64                 }
 65             })
 66         </script>
 67     </div>
 68     <!-- prop -->
 69     <div>
 70         <div id="app2">
 71             <!-- prop是组件上注册的自定义的attribute,
 72                 当一个值传递给一个prop attribute的时候,
 73                 它变成了那个组件实例的一个property
 74             -->
 75             <component_4 title="This is prop4_1"></component_4>
 76             <component_4 title="This is prop4_2"></component_4>
 77             <component_4 title="This is prop4_3"></component_4>
 78             <!--每个组件只能有一个根目录, 
 79                 当想传入多个数据时,可将数据以数组形式写入data内,
 80                 用v-for遍历,v-bind绑定数组,之后在组件中的props调用
 81                 组件中的数据就可以调用data中数组的值
 82             -->
 83             <component_5 v-for="post in posts1" :keys="post.id" :title="post.value" ></component_5>
 84             <component_6 v-for="post in posts2" :key="post.id" :post="post"></component_6>
 85             <!--可直接写入属性,如类,样式
 86                 
 87             -->
 88             <component_7 class="this" style="color:blue"></component_7>
 89         </div>
 90         <script>
 91             Vue.component('component_4',{
 92                 props:['title'],
 93                 template:'<h2>{{title}}</h2>'
 94             })
 95             Vue.component('component_5',{
 96                 props:['title','keys'],
 97                 template:'<h2>This is {{keys}}  No.{{title}}</h2>'
 98             })
 99             Vue.component('component_6',{
100                 props:['post'],
101                 template:'<h2>this is {{post.id}} , No.{{post.value}}</h2>'
102             })
103             Vue.component('component_7',{
104                 // 如不想继承组件的属性,可使用:inheritAttrs:flase
105                 template:'<h2>this is Component_7</h2>'
106             })
107             new Vue({
108                 el:"#app2",
109                 data:{
110                     posts1:[
111                         {id:'5-1',value:'prop5_1'},
112                         {id:'5-2',value:'prop5_2'},
113                         {id:'5-3',value:'prop5_3'}
114                     ],
115                     posts2:[
116                         {id:'6-1',value:'prop6_1'},
117                         {id:'6-2',value:'prop6_2'},
118                         {id:'6-3',value:'prop6_3'}
119                     ]
120                 }
121             })
122         </script>
123     </div>
124 </body>
125 </html>

免责声明:文章转载自《Vue-组件(二)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇redis 3 主从节点保证数据一致性+ 数据持久化(RDB+AOF)【linux】suse linux 常用命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vuejs绑定img 的src

1.显示本地图片: <img src="http://t.zoukankan.com/common/images/auth-icon.png" />   2.绑定变量: <img class="" :src="http://t.zoukankan.com/defaultIcon" /> data() {   return {  ...

windows下vue项目启动步骤

原创:https://blog.csdn.net/qq_27680317/article/details/71123051?locationNum=10&fps=1  不是ngnix服务器是,忽略7~10; 前后端分离项目,要做前后端联动测试,没整过前端用vue, nodejs和webpack的开发管理方式。来回折腾终于把已经建好的vue项目启动起...

vue跨域访问

第一次创建vue项目,画完静态页面一切顺利,准备和后台进行联调,问题来了,无论怎么调试使用Axios,jQuary还是使用原生的Ajax请求都访问不通(前提条件,另外一个人的电脑当成服务器,进行访问),然后各种百度查询了很多资料才明白我进行了跨域访问, 解决办法,修改config/index.js和config/prod.env.js文件 对config/...

VUE集成keycloak和Layui集成keycloak

一:KEYCLOAK配置部分:   1,下载keycloak,官网地址:https://www.keycloak.org/downloads.html。下载第一个就行        2,下载完毕之后,打开文件,访问 bin 路径,点击 standalone.bat 打开,打开之后大概如下:             3,然后访问:http://localho...

vue版本更新

之前电脑已经安装 Node环境和 vue-cli脚手架,但是过段时间没有使用,然后现在用 vue-cli 搭建项目的时候,启动服务器的时候报错,无法启动成功,摸索半天,发现是因为 Node和vue-cli 的版本过低,都需要更新,更新过后成功启动...... 以下是 Node 和 vue-cli 更新的总结: 一. vue-cli更新   在百度了好久,...

vue前端工程化

今日目标 1.能够了解模块化的相关规范 2.了解webpack3.了解使用Vue单文件组件4.能够搭建Vue脚手架5.掌握Element-UI的使用 1.模块化的分类 A.浏览器端的模块化 1).AMD(Asynchronous Module Definition,异步模块定义)代表产品为:Require.js2).CMD(Common Module D...