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 功能的编写,在熟悉基 ...
随机推荐
- Docker Compose demo 使用
1.docker compose 安装 curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker- ...
- CHAPITRE III
Il me fallut longtemps pour comprendre d'où il venait. Le petit prince, qui me posait beaucoup de qu ...
- Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)
https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...
- 证明LDU分解的唯一性
首先上(下)三角矩阵乘以上(下)三角矩阵结果还是上(下)三角矩阵, 另外我们考虑相乘后的对角元素可发现,对角原始是原来2矩阵对应对角元素的乘积. 另外对角线都是1的上(下)三角矩阵必定可以只是用行运算 ...
- date 工具类
package lizikj.bigwheel.common.vo.merchandise.util; import java.text.DateFormat; import java.text.Pa ...
- vue的cli中引入css文件
在public文件中创建一个文件夹css,放进reset.css 在main.js中引入即可 import '../public/css/reset.css'就可以啦
- mysql学习之路_sql
查看数据库: Show databases; 查看指定部分数据库:模糊查询 Show databases like ‘patten’;--paatten是匹配模式 %:表示是匹配模式 _:表示匹配单个 ...
- mybatis 时间区间比较
直接上代码,此时数据库使用的Date类型: <if test="minCreateTime != null and minCreateTime != ''"> < ...
- Gym 100096D Guessing game
Gym 100096D Guessing game 题面 Problem Description Byteman is playing a following game with Bitman. Bi ...
- hdu 4939 三色塔防
http://acm.hdu.edu.cn/showproblem.php?pid=4939 给出一条长为n个单位长度的直线,每通过一个单位长度需要 t 秒. 有3种塔,红塔可以在当前格子每秒造成 x ...