主要内容:如何注册组件、如何使用组件、父组件子组件之间值的传递、具名插槽

1.如何注册组件

第一步:通过import将子组件载入父组件的js中

// 第一步:通过import将子组件载入父组件的js中
import child from './components/child.vue'
import childB from './components/childB.vue'

第二步:将子组件在父组件的componerts中注册

  components: {child,childB},

如果想给引入的组建重新命名,那么:

  components: {新名字:child},

2.如何使用组件

引入组件的第一种写法

<template><div id="app"><!-- 如果注册的组建名中有大写字母,尽量改为 -小写字母,例如:childB,则在使用组件时,要用:<child-b></child-b>--><child></child><child-b></child-b></div>
</template>

引入组件的第二种写法

<template><div id="app"><div :is="comToRender" @my-event="onChildEvent"></div><button @click="changeFun">切换组建</button></div>
</template>

优点:

1.DOM 模板解析注意事项

2.可以动态切换组建 通过v-bind:is

实例:

目录:

父组件:app.vue

<!-- 父组件如何加渲染子组 -->
<template><div id="app"><!-- 第三步:使用组件 --><!-- 引入组件的第一种写法 --><!-- 如果注册的组建名中有大写字母,尽量改为 -小写字母,例如:childB,则在使用组件时,要用:<child-b></child-b>--><!-- <child></child><child-b></child-b> --><!--引入组件的第二种写法--><!-- 优点:1.https://cn.vuejs.org/v2/guide/components.html#DOM-%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%90%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B92.可以动态切换组建 通过v-bind:is--><div :is="comToRender" @my-event="onChildEvent"></div><button @click="changeFun">切换组建</button></div>
</template>
<script>
// 第一步:通过import将子组件载入父组件的js中
import child from './components/child.vue'
import childB from './components/childB.vue'
export default {name: 'app',// 第二步:将子组件在父组件的componerts中注册// 如果想给引入的组建重新命名,那么:// components: {//   新名字:child// },
  components: {child,childB},data() {return {comToRender: 'child',fatherString:'hello children',parameterNum:'1'}},mounted: function() {this.$nextTick(function() {});},methods: {onChildEvent(parmFromChild) {console.log(parmFromChild);},changeFun(){this.comToRender="childB";}}
}</script>
<style>
* {margin: 0;padding: 0;
}body {width: 100%;height: 100%;position: absolute;
}#app {font-family: 'Avenir', Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;width: 100%;height: 100%;
}</style>

子组件:child.vue

<template><div class="child-conten"><p>{{msg}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {  data() {return {msg: 'I am a components'}},methods: {emitMyEvent() {this.$emit('my-event', this.msg);}}
}</script>

子组件:childB.vue

<template><div class="child-conten"><p>{{msg}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {data(){return{msg: 'I am a components children B'}},methods:{emitMyEvent(){this.$emit('my-event',this.msg);}}
}</script>

页面效果:

3.父组件子组件之间值的传递

使用 Prop 传递数据

1.传递固定值:

父组件:app.vue

<template><div id="app"><child number="5" @my-event="onChildEvent"></child></div>
</template>
<script>
// 第一步:通过import将子组件载入父组件的js中
import child from './components/child.vue'
export default {name: 'app',components: {child},data() {return {}},mounted: function() {this.$nextTick(function() {});},methods: {onChildEvent(parmFromChild) {console.log(parmFromChild);}}
}</script>

子组件:child.vue

<template><div class="child-conten"><p>{{msg}}</p><p>我是父组件传递过来的值:{{number}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {// props属性内容的简写:
  props:['number'],data() {return {msg: 'I am a components'}},methods: {emitMyEvent() {this.$emit('my-event', this.msg);}}
}</script>

注意:如果传递的参数存在大写字母

父组件:app.vue

<template><div id="app"><child number-to-do="5" @my-event="onChildEvent"></child></div>
</template>

子组件:child.vue

<template><div class="child-conten"><p>{{msg}}</p><p>我是父组件传递过来的值:{{numberToDo}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {// props属性内容的简写:
  props: ['number-to-do'],data() {return {msg: 'I am a components'}},// 注意 mounted 不会承诺所有的子组件也都一起被挂载。如果你希望等// 到整个视图都渲染完毕,可以用 vm.$nextTick 替换掉 mounted:
  mounted: function() {this.$nextTick(function() {console.log('this.numberToDo:' + this.numberToDo);})},methods: {emitMyEvent() {this.$emit('my-event', this.msg);}}
}</script>

2.动态prop:

父组件:app.vue

<template><div id="app"><input type="text" v-model="fatherString"><child :number-to-do='fatherNum' :string='fatherString' @my-event="onChildEvent"></child></div>
</template>
<script>
// 第一步:通过import将子组件载入父组件的js中
import child from './components/child.vue'
export default {name: 'app',components: {child},data() {return {fatherNum:100,fatherString:'hello children'}},mounted: function() {this.$nextTick(function() {});},methods: {onChildEvent(parmFromChild) {console.log(parmFromChild);}}
}</script>

子组件:child.vue

<template><div class="child-conten"><p>{{msg}}</p><p>我是父组件传递过来的值:{{numberToDo}}</p><p>我是父组件传递过来的值:{{string}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {// props属性内容的简写:
  props: ['number-to-do', 'string'],data() {return {msg: 'I am a components'}},// 注意 mounted 不会承诺所有的子组件也都一起被挂载。如果你希望等// 到整个视图都渲染完毕,可以用 vm.$nextTick 替换掉 mounted:
  mounted: function() {this.$nextTick(function() {console.log('this.numberToDo:' + this.numberToDo);})},methods: {emitMyEvent() {this.$emit('my-event', this.msg);}}
}</script>

页面效果:

3.Prop 验证

type 可以是下面原生构造器:

  • String
  • Number
  • Boolean
  • Function
  • Object
  • Array
  • Symbol

type 也可以是一个自定义构造器函数,使用 instanceof 检测。

当 prop 验证失败,Vue 会抛出警告 (如果使用的是开发版本)。注意 prop 会在组件实例创建之前进行校验,所以在 defaultvalidator 函数里,诸如 datacomputedmethods 等实例属性还无法使用。

父组件:app.vue

<template><div id="app"><!-- number-to-do不传,则子组件的使用默认值 --><child @my-event="onChildEvent"></child><!-- 子组件中定义number-to-do type: Number ,如果传入不是number类型的参数,则会报错--><child :number-to-do='fatherNum' @my-event="onChildEvent"></child></div>
</template>
<script>
// 第一步:通过import将子组件载入父组件的js中
import child from './components/child.vue'
export default {name: 'app',components: {child},data() {return {fatherNum: 2,}},mounted: function() {this.$nextTick(function() {});},methods: {onChildEvent(parmFromChild) {console.log(parmFromChild);}}
}</script>

子组件:child.vue

<template><div class="child-conten"><p>{{msg}}</p><p>我是父组件传递过来的值:{{numberToDo}}</p><button @click='emitMyEvent'>child-emit</button></div>
</template>
<script>
export default {props: {'number-to-do': {type: Number,default: 100}},data() {return {msg: 'I am a components'}},methods: {emitMyEvent() {this.$emit('my-event', this.msg);}}
}</script>

通过以上实例,发现子组件改变父组件的值是通过:子组件通过this.$emit监听父组件的 my-event,将子组件的值传递给父组件。

4.具名插槽

父组件:

<template><div id="app"><input type="text" v-model="fatherString"><child :number-to-do='fatherNum' :string='fatherString' @my-event="onChildEvent"><p slot='header'>xxxxx header</p><p slot='footer'>xxxxx footer</p></child></div>
</template>

子组件:

<template><div class="child-conten"><p>{{msg}}</p><!-- <p>我是父组件传递过来的值:{{numberToDo}}</p><p>我是父组件传递过来的值:{{string}}</p> --><button @click='emitMyEvent'>child-emit</button><br><slot name='header'>no header</slot><p>子组件内容</p><slot name='footer'>no header</slot></div>
</template>

页面效果:

git地址:vue2.0Demo

转载于:https://www.cnblogs.com/yingzi1028/p/8250876.html

vue2.0笔记《二》组件相关推荐

  1. vue2.0实现分页组件

    http://www.php361.com/index.php?c=index&a=view&id=4671 http://www.yanglajiao.com/article/sin ...

  2. vue2.0桌面端框架_Element-UI组件库(Vue2.0桌面端组件库)V2.9.2 免费版

    Element-UI组件库(Vue2.0桌面端组件库)是一款很优秀好用的为开发者.设计师和产品经理推出的基于Vue 2.0的桌面端组件库软件.小编带来的这款Element-UI组件库功能强大全面,简单 ...

  3. vue tree组件_基于 Vue2.0 和 HeyUI 组件库的中后端系统 HeyUI Admin

    介绍 heyui-admin 是一个成熟的企业应用解决方案,基于 vue2.0 和 heyui 组件库的中后端系统. 功能 - Js - common / 通用 - ajax / 封装axios - ...

  4. vue2.0 如何自定义组件(vue组件的封装)

    一.前言 之前的博客聊过 vue2.0和react的技术选型:聊过vue的axios封装和vuex使用.今天简单聊聊 vue 组件的封装. vue 的ui框架现在是很多的,但是鉴于移动设备的复杂性,兼 ...

  5. vue结合饿了么_饿了么基于Vue2.0的通用组件开发之路(分享会记录)

    Element:一套通用组件库的开发之路 Element 是由饿了么UED设计.饿了么大前端开发的一套基于 Vue 2.0 的桌面端组件库.今天我们要分享的就是开发 Element 的一些心得. 官网 ...

  6. 尚硅谷天禹老师Vue2.0笔记

    笔记 关于不同版本的 Vue: vue.js 与 vue.runtime.xxx.js 的区别: vue.js是完整版的Vue,包含 核心功能 + 模板解析器 vue.runtime.xxx.js 是 ...

  7. vue2.0 class声明组件_案例精选 | 蘑菇街、滴滴、淘宝、微信的组件化架构解析

    导读:前段时间公司项目打算重构,准确来说应该是按之前的产品逻辑重写一个项目.在重构项目之前涉及到架构选型的问题,我和组里小伙伴一起研究了一下组件化架构,打算将项目重构为组件化架构.当然不是直接拿来照搬 ...

  8. 尚硅谷Vue2学习笔记 (二)

    脚手架文件结构 node_modules publicfavicon.ico 页签图标index.html 主页面 srcassets 存放静态资源logo.png component 存放组件Hel ...

  9. vue组件中嵌套html,vue2.0怎么用组件自定义标签实现组件的嵌套?

    想用这种方式实现组件嵌套: 目前实现的方式:是在app-content.vue中的template中嵌套的 index.html main.js import Vue from '../node_mo ...

最新文章

  1. 加速数据中心变革,Xilinx推出软件定义、硬件加速型 Alveo SmartNIC
  2. myeclipse6.0下载及注冊码
  3. 黄聪:说说JSON和JSONP,也许你会豁然开朗(转)
  4. 使用 LocalDateTime 而不是 Date
  5. 最小表示法 最大表示法
  6. nginx系列之六:cache服务
  7. 模型融合(stackingblending)
  8. 转:金牌网管师初级网络实验手册
  9. Codeforces 659B Qualifying Contest【模拟,读题】
  10. [Unity] 在协程中等待指定的毫秒
  11. SPSS基础教程:统计分析前的准备
  12. 分享一个特别好用的时间选择控件
  13. TortoiseSVN简明教程
  14. 解除操作系统宽带限制
  15. 头像设计,如何用PS制作个性头像
  16. iPhone 8价格已破发!苹果无奈只能减产
  17. 判断点是否在点组成的封闭区域内c++
  18. python前戏之量
  19. 2019机器学习框架之争:与Tensorflow竞争白热化,进击的PyTorch赢在哪里?
  20. Win10彻底删除Windows.old文件夹

热门文章

  1. Go简单的Goroutine示例
  2. 《HTML5多媒体应用开发》——第2章 HTML5多媒体元素2.1 Web多媒体历史
  3. 制作空镜像与一个run的共享文件错误解决
  4. 使用Lua编写whireshark插件
  5. linux下初次安装mysql使用指南
  6. 实用windows short cut
  7. Spring Boot 中的 RestTemplate 不好用?试试 Retrofit!
  8. 我的年龄又快被5整除了......
  9. 京东秒杀系统模块的Redis分布式锁深度剖析,没给你讲明白你打我
  10. 分布式锁选型背后的架构设计思维【附源码】