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 第四章笔记 下的更多相关文章

  1. Vue2.5开发去哪儿网App 第五章笔记 下

    1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...

  2. Vue2.5开发去哪儿网App 第四章笔记 上

    一 .  组件细节知识点 1.  解决组件在h5中编码规范 例如 : table , ul , ol  等等 <table> <tbody> <row></r ...

  3. Vue2.5开发去哪儿网App 第三章笔记 下

    1.样式的绑定 我们可以传给 v-bind:class 一个对象,以动态地切换 class   例如: :class="{activated:isactivated}" 上面的语法 ...

  4. Vue2.5开发去哪儿网App 第五章笔记 上

    1.css动画原理 .fade-enter{ opacity: 0; } .fade-enter-active{ transition: opacity 2s; } .fade-leave-to{ o ...

  5. Vue2.5开发去哪儿网App 第三章笔记 上

    1.  vue 生命周期函数 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 ...

  6. Vue2.5 开发去哪儿网App

    Vue2.5开发去哪儿网App 技术栈和主要框架

  7. Vue2.5开发去哪儿网App 首页开发

    主页划 5 个组件,即 header  icon  swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...

  8. Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用

    一,数据共享 1.  安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...

  9. Vue2.5开发去哪儿网App 从零基础入门到实战项目

    第1章 课程介绍本章主要介绍课程的知识大纲,学习前提,讲授方式及预期收获. 1-1 课程简介 试看第2章 Vue 起步本章将快速讲解部分 Vue 基础语法,通过 TodoList 功能的编写,在熟悉基 ...

随机推荐

  1. 2018.11.01 loj#2319. 「NOIP2017」列队(线段树)

    传送门 唉突然回忆起去年去noipnoipnoip提高组试水然后省二滚粗的悲惨经历... 往事不堪回首. 所以说考场上真的有debuffdebuffdebuff啊!!!虽然当时我也不会权值线段树 这道 ...

  2. Effective C++ 随笔(3)

    条款12: 以对象管理资源 两种只能指针: std:auto_ptr<> 当使用copy操作室,原先的智能指针指向为null std:tr1:share_ptr<int> sp ...

  3. spring boot和mybatis入门

    [size=x-large]昨天讲了一下spring boot的入门操作相信老手已经明白入门的操作,今天我来讲下我自己的心得,可能与官方有一定差异:希望对大家能有用 一:开门见山首先看工程结构 这里的 ...

  4. linux搭建简易版本的FastDFS服务器

    开发环境:centos7环境 搭建FastDFS集群搭建非常复杂,对于初期学习FastDFS来说,搭建个单机版的作为入门更为实际一些. 首先感谢“在京奋斗者“”博主的详细搭建过程,附上博客地址http ...

  5. Chapter14 糖酵解 糖异生 戊糖途径

    糖酵解 一. 1.总览 产生丙酮酸 产生两个ATP 产生两个NADH 丙酮酸的去路: 产生乙醇 产生乳酸 乙酰辅酶A 参与其他合成 2.氧化磷酸化和之后的底物水平磷酸化是一个藕联过程 3.能量存在丙酮 ...

  6. linux下的各个目录(待填)

    /系统目录 / 下的目录: 1./bin(binary,二进制文件):打开里面会发现全是绿色的文件,也就是可执行文件,而且名字都是系统命令的名字,其实每个系统命令都是一个小的可执行的文件,这些命令都存 ...

  7. Codeforces Round #540 (Div. 3)--1118B - Tanya and Candies(easy TL!)

    Tanya has nn candies numbered from 11 to nn. The ii-th candy has the weight aiai. She plans to eat e ...

  8. Yeoman安装和使用详解

    Yeoman generator-react-webpack 一 什么是Yeoman Yeoman帮助我们创建项目,提供更好的工具来使我们的项目更多样化. Yeoman提供generator系统,一个 ...

  9. hdu 4927 组合+公式

    http://acm.hdu.edu.cn/showproblem.php?pid=4927 给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1−ai,直到序列长度 ...

  10. Nvu

    在Jennifer Niederst Robbins的书<Learning Web design>(密码:v9i1)推荐软件Nvu 界面: Nvu tips: