Vue2.5开发去哪儿网App 第四章笔记 下
1.解决非父子组件之间的传值问题
非父子组件传值(Bus/总线/发布订阅模式/观察者模式)
给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性
Vue.prototype.bus = new Vue(); //发送
this.bus.$emit('change',this.selfContent);
//监听
this.bus.$on('change',function (value) {
this_.selfContent = value;
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>非父子组件传值(Bus/总线/发布订阅模式/观察者模式)</title>
</head>
<body>
<script src="../../vue.js"></script>
<div id="app">
<child content="dong"></child>
<child content="hao"></child>
</div>
<script>
Vue.prototype.bus = new Vue(); Vue.component('child',{
data:function(){
return {
selfContent:this.content
}
},
props:{
content:String,
},
template:'<div @click="HandleClick">{{selfContent}}</div>',
methods:{
HandleClick:function () {
this.bus.$emit('change',this.selfContent);
}
},
// #组件被挂载时候执行
mounted:function () {
var this_ = this
this.bus.$on('change',function (value) {
this_.selfContent = value;
});
}
});
var vm = new Vue({
el:"#app"
})
</script>
</body>
</html>
2. 插槽
使用:
<child>
<!--定义插槽-->
<p>dell</p>
<p>dell</p>
<p>dell</p>
</child>
//slot标签使用插槽
Vue.component('child',{
props:['content'],
template:`<div>
<p>hello</p>
<slot></slot>
</div>`
})
默认插槽:
<default></default>
Vue.component('default',{
template:`<div>
<slot>我是默认值</slot>
</div>`
})
具名插槽:
<body-content>
<!--具名插槽-->
<div class="header" slot="header">header</div>
<div class="footer" slot="footer">footer</div>
</body-content>
// #header-footer
// 当不传时,使用默认值
Vue.component("body-content",{
template:`<div>
<slot name="header"></slot>
<div class="content">content</div>
<slot name="footer"></slot>
<slot name="default-header">
<h1>默认header</h1>
</slot>
</div>`
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用插槽(slot)</title>
<script src="../../vue.js"></script>
</head>
<div id="app">
<child>
<!--插槽-->
<p>dell</p>
<p>dell</p>
<p>dell</p>
</child> <!--#默认值-->
<default></default> <!--// #header-footer-->
<body-content>
<!--具名插槽-->
<div class="header" slot="header">header</div>
<div class="footer" slot="footer">footer</div>
</body-content>
</div>
<script>
Vue.component('child',{
props:['content'],
template:`<div>
<p>hello</p>
<slot></slot>
</div>`
}) Vue.component('default',{
template:`<div>
<slot>我是默认值</slot>
</div>`
}) // #header-footer
// 当不传时,使用默认值
Vue.component("body-content",{
template:`<div>
<slot name="header"></slot>
<div class="content">content</div>
<slot name="footer"></slot>
<slot name="default-header">
<h1>默认header</h1>
</slot>
</div>`
}) var vm =new Vue({
el:"#app"
})
</script>
<body>
</body>
</html>
3. 作用域插槽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>作用域插槽</title>
<script src="../../vue.js"></script>
</head>
<body>
<div id="app">
<child>
<!--作用域插槽 template-->
<template slot-scope="props">
<h1>{{props.item}}</h1>
</template>
</child>
<!--dom结构由外部传递-->
<!--使用template标签,并且用slot-scope接收-->
</div>
<script>
Vue.component('child',{
data:function(){
return {
list:[1,2,3,4,5,6]
}
},
// :item="item"子组件向父组件中传递数据
template:`<div>
<ul>
<slot v-for="item in list" :item="item">
</slot>
</ul>
</div>`
})
var vm = new Vue({
el:"#app"
})
</script>
</body>
</html>
4.动态组件和v-once指令
使用component标签 动态显示组件
<component :is="type"></component>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态组件和v-once指令</title>
<script src="../../vue.js"></script>
</head>
<body>
<div id="app">
<!--<child-one v-if="type==='child-one'"></child-one>-->
<!--<child-two v-if="type==='child-two'"></child-two>--> <component :is="type"></component>
<!--耗费性能,重复销毁 创建-->
<!--v-once直接放到内存里-->
<button @click="handleBtnclick">click me</button>
</div> <script>
Vue.component('child-one',{
template:`<div v-once>this is child one</div>`
}) Vue.component('child-two',{
template:`<div v-once>this is child two</div>` })
var vm = new Vue({
el:"#app",
data:{
type:'child-one'
},
methods:{
handleBtnclick:function () {
this.type = this.type==='child-one'?'child-two':'child-one'
}
}
})
</script>
</body>
</html>
Vue2.5开发去哪儿网App 第四章笔记 下的更多相关文章
- Vue2.5开发去哪儿网App 第五章笔记 下
1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...
- Vue2.5开发去哪儿网App 第四章笔记 上
一 . 组件细节知识点 1. 解决组件在h5中编码规范 例如 : table , ul , ol 等等 <table> <tbody> <row></r ...
- Vue2.5开发去哪儿网App 第三章笔记 下
1.样式的绑定 我们可以传给 v-bind:class 一个对象,以动态地切换 class 例如: :class="{activated:isactivated}" 上面的语法 ...
- Vue2.5开发去哪儿网App 第五章笔记 上
1.css动画原理 .fade-enter{ opacity: 0; } .fade-enter-active{ transition: opacity 2s; } .fade-leave-to{ o ...
- Vue2.5开发去哪儿网App 第三章笔记 上
1. vue 生命周期函数 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 ...
- Vue2.5 开发去哪儿网App
Vue2.5开发去哪儿网App 技术栈和主要框架
- Vue2.5开发去哪儿网App 首页开发
主页划 5 个组件,即 header icon swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...
- Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用
一,数据共享 1. 安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...
- Vue2.5开发去哪儿网App 从零基础入门到实战项目
第1章 课程介绍本章主要介绍课程的知识大纲,学习前提,讲授方式及预期收获. 1-1 课程简介 试看第2章 Vue 起步本章将快速讲解部分 Vue 基础语法,通过 TodoList 功能的编写,在熟悉基 ...
随机推荐
- mybatis中文官网
http://www.mybatis.org/mybatis-3/zh/index.html
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)
https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...
- linux下的wireshark最新版安装(源码安装)以及一些常见问题
源码安装教程 http://www.cnblogs.com/littleTing/p/3765589.html 1.下载wireshark: 网址:http://www.wireshark.org/d ...
- word粘贴图片+的editor
公司做的项目需要用到文本上传功能. Chrome+IE默认支持粘贴剪切板中的图片,但是我要粘贴的文章存在word里面,图片多达数十张,我总不能一张一张复制吧? 我希望打开文档doc直接复制粘贴到富文本 ...
- rhel5.4+oracle 10g rac
各种报错各种愁啊 ... 1> 不知道什么原因,在节点2执行root.sh 报错 .无解 . 还原虚拟机,重新安装 .唯一与以前不同的是,执行orainroot.sh后 接着在节点2执行.再去分 ...
- Phalanx (hdu 2859)
http://acm.hdu.edu.cn/showproblem.php?pid=2859 Time Limit: 10000/5000 MS (Java/Others) Memory ...
- HBase最佳实践(好文推荐)
HBase最佳实践-写性能优化策略 HBase最佳实践-管好你的操作系统 HBase最佳实践之列族设计优化 [大数据]HBase最佳实践 – 集群规划
- 1.buntu 安装redis
方式一 :apt安装 在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 R ...
- C# 日志输出工具库—log4net 安装、配置及简单应用
1.下载和安装 注意每次安装只是安装到本项目中,换了另一个项目需要再次安装和配置. 我使用的是Visual Studio 2013 社区版,在tools中找到NuGet包管理. 搜索log4net并点 ...
- 【Win】Clso QR Tool 二维码小工具
一个可以生成并识别二维码的windows小工具,纯绿色.不含糖. 可以通过输入文本生成二维码,或者加载本地图片.剪贴板内的图片,直接解析出二维码内容. 支持自定义LOGO. 下载文件 (当前版本:1. ...