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 功能的编写,在熟悉基 ...
随机推荐
- python 安装教程
1) 安装python2.7,下载地址 https://www.python.org/downloads/ ----2.7 安装完成后,设置环境变量加入path --d:/ruanjian/p ...
- Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)
31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...
- JS中的计时器事件
JS可以实现很多java代码不易完成的功能.这里学习一些js中的计时器事件. JavaScript 一个设定的时间间隔之后来执行代码,称之为计时事件. 主要通过两个方法来实现: 1.setInterv ...
- python中的分号
很多编程语言是以分号作为一行代码的的结束标志,但是在Python中不是这样的,而是靠缩进来识别程序结构. Python中一行代码以分号结束,并不是必须的,准确来说是不被推荐的,因为加上分号就是画蛇添足 ...
- 关于内存类型 UDIMM、RDIMM、LRDIMM 的学习结论(转)
随着内存技术不断发展,服务器上内存的容量.密度和速度也越来越高.目前在市场上出现的内存条最高密度可以做到每条内存条 4 个 Rank,容量达到 32GB/条,最高速度达到 1.6GHz.高密度高频率也 ...
- Android 全局搜索条写成自定义控件-曹永思
图文: 1.Android 自定义控件的布局文件 2.编写Android 自定义控件的要处理的逻辑代码(曹永思) 3.在调用自定义控件的 Activity的布局文件中调用Android 称之为控件,控 ...
- linux内核链表使用
原文链接:http://blog.csdn.net/xnwyd/article/details/7359373 Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p ...
- (转)ASP.NET MVC 3和Razor中的@helper 语法
转自:http://kb.cnblogs.com/page/102191/ ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写 ...
- (转)php语法(符号用法)
转自:http://blog.unvs.cn/archives/php-equal-bracket.html 学习PHP过程中,会常碰到一些特殊的符号,比如:=.==.===.->.=>. ...
- _编程语言_C++_宏定义#define 和 常量const 的区别
C++中有两种定义常量的方式:#define预处理和const关键字 #define 预处理指令 #include <iostream> using namespace std; #def ...