template模板引用

在component的template中书写大量的HTML元素很麻烦。 Vue提供了<template>标签,可以在里边书写HTML,然后通过ID指定到组建内的template属性上;

示例:

由图可知自定义组件的count的值是自增的,是独立的,互不影响。

vue代码:

<template id="my-template">

        <div>
<h2>{{name}}</h2>
<button @click="count++">{{count}}</button>
<ul> <li v-for="item in numArray">{{item}}</li>
</ul> </div>
</template>
<script type="text/javascript" src="../js/vue.js" ></script>
<script> new Vue({ components:{
"my-component-b":{
template:'#my-template',
data(){
return{
name:'欢迎来到perfect*的博客园_01',
numArray:[,,,,],
count:
}
} }
} }).$mount('div');
</script>

html:

<body>
<div>
<my-component-b></my-component-b><!--可以把my-component-b看做一个对象-->
<my-component-b></my-component-b><!--可以把my-component-b看做一个对象--> </div>
</body>

动态组件:

在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件;

动态组件的使用:

需要使用内置组件<component></component>,根据 :is 的值决定显示哪个组件,:is的值是要显示的组件id;

示例:

初始效果:

初始代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>动态组件</title>
</head>
<body>
<div>
<my-component-a></my-component-a>
<my-component-b></my-component-b>
<my-component-c></my-component-c> </div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script> new Vue({ components:{
"my-component-a":{
template:'<h1>欢迎来到perfect*的博客园</h1>' }, "my-component-b":{
template:"<a href='https://www.cnblogs.com/jiguiyan/'> perfect*的博客园 </a>" },
"my-component-c":{
template:"<p>perfect*</p>" }, } }).$mount('div');
</script>
</html>

动态组件

现在的需求:

需要在页面中只显示一个,并通过三个button来进进行控制它们的显示

由效果图可知,页面默认显示my-component-a标签的内容

vue代码:

<script>

        new Vue({
data:{
selectName:'my-component-a' }, components:{
"my-component-a":{
template:'<h1>欢迎来到perfect*的博客园</h1>' }, "my-component-b":{
template:"<a href='https://www.cnblogs.com/jiguiyan/'> perfect*的博客园 </a>" },
"my-component-c":{
template:"<p>perfect*</p>" }, } }).$mount('div');
</script>

html:

    <div>
<button @click="selectName='my-component-a' ">a</button>
<button @click="selectName='my-component-b' ">b</button>
<button @click="selectName='my-component-c' ">c</button>
<!--<my-component-a></my-component-a>
<my-component-b></my-component-b>
<my-component-c></my-component-c>--> <component :is="selectName"></component> </div>

代码注意:

总的代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>动态组件</title>
</head>
<body>
<div>
<button @click="selectName='my-component-a' ">a</button>
<button @click="selectName='my-component-b' ">b</button>
<button @click="selectName='my-component-c' ">c</button>
<!--<my-component-a></my-component-a>
<my-component-b></my-component-b>
<my-component-c></my-component-c>--> <component :is="selectName"></component> </div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script> new Vue({
data:{
selectName:'my-component-a' }, components:{
"my-component-a":{
template:'<h1>欢迎来到perfect*的博客园</h1>' }, "my-component-b":{
template:"<a href='https://www.cnblogs.com/jiguiyan/'> perfect*的博客园 </a>" },
"my-component-c":{
template:"<p>perfect*</p>" }, } }).$mount('div');
</script>
</html>

动态组件

动态组件结合keep-alive

keep-alive:将非活动的组件缓存起来

include - 字符串或正则表达式。只有名称匹配的组件会被缓存。

exclude - 字符串或正则表达式。任何名称匹配的组件都不会被缓存。

max - 数字。最多可以缓存多少组件实例。

示例:

初始效果:

由图可以看出每一次点击button调用的值不一样,因此引入了keep-alive来进行缓存

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>动态组件</title>
</head>
<body>
<div>
<button @click="selectName='my-component-a' ">a</button>
<button @click="selectName='my-component-b' ">b</button>
<button @click="selectName='my-component-c' ">c</button>
<!--<my-component-a></my-component-a>
<my-component-b></my-component-b>
<my-component-c></my-component-c>--> <component :is="selectName"></component> </div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script> new Vue({
data:{
selectName:'my-component-a' }, components:{
"my-component-a":{
template:'<h1>A:{{num}}</h1>',
data(){ return{
num:Math.ceil(Math.random()*)
}
} }, "my-component-b":{
template:"<a href='#'>B:{{num}} </a>",
data(){ return{
num:Math.ceil(Math.random()*)
}
} },
"my-component-c":{
template:"<p>C:{{num}}</p>",
data(){ return{
num:Math.ceil(Math.random()*)
}
} }, } }).$mount('div');
</script>
</html>

初始代码

从图中可以看出 a:1 b:84 c: 86的值一直没发生改变,说明已经被缓存了。

include属性:

只有a的值被缓存了

<keep-alive include="my-component-a"><!--只有a的值被缓存了-->

            <component :is="selectName"></component>
</keep-alive>

可以通过数组进行多个:

效果:

缓存了a和b的值

<keep-alive :include="['my-component-a','my-component-b']"><!--a,b的值被缓存了-->

            <component :is="selectName"></component>
</keep-alive>

同理exclude 属性测试方法和include一样,只是exclude表示的是除了那一个不缓存

max属性:最多可以缓存多少组件实例

效果图:

<keep-alive :max=''><!--最多只能缓存abc三个值中的其中两个-->
<component :is="selectName"></component>
</keep-alive>

总的代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>动态组件</title>
</head>
<body>
<div>
<button @click="selectName='my-component-a' ">a</button>
<button @click="selectName='my-component-b' ">b</button>
<button @click="selectName='my-component-c' ">c</button>
<!--<my-component-a></my-component-a>
<my-component-b></my-component-b>
<my-component-c></my-component-c>-->
<!--<keep-alive include="my-component-a"><!--只有a的值被缓存了--> <!--<keep-alive :include="['my-component-a','my-component-b']"><!--a,b的值被缓存了-->
<keep-alive :max=''><!--最多只能缓存abc三个值中的其中两个-->
<component :is="selectName"></component>
</keep-alive> </div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script> new Vue({
data:{
selectName:'my-component-a' }, components:{
"my-component-a":{
template:'<h1>A:{{num}}</h1>',
data(){ return{
num:Math.ceil(Math.random()*)
}
} }, "my-component-b":{
template:"<a href='#'>B:{{num}} </a>",
data(){ return{
num:Math.ceil(Math.random()*)
}
} },
"my-component-c":{
template:"<p>C:{{num}}</p>",
data(){ return{
num:Math.ceil(Math.random()*)
}
} }, } }).$mount('div');
</script>
</html>

动态组件结合keep-alive

详细介绍官网网址:

https://cn.vuejs.org/v2/api/#keep-alive

Vue 组件&组件之间的通信 之 template模板引用与动态组件的使用的更多相关文章

  1. vue组件父子之间相互通信案例

  2. vue组件---动态组件&异步组件

    (1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...

  3. Vue组件-动态组件

    动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...

  4. 三、深入Vue组件——Vue插槽slot、动态组件

    一.插槽slot() 1.1简单插槽slot [功能]用于从父组件中,通过子组件写成双标签,向子组件中放入自定的内容 parent.vue [1]首先把child写成双标签样式,把要插入的内容放双标签 ...

  5. Vue_(组件通讯)动态组件结合keep-alive

    keep-alive 传送门 <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们.和 <transition> 相似,<keep-alive ...

  6. Angular 学习笔记 (动态组件 & Material Overlay & Dialog 分析)

    更新: 2019-11-24  dialog vs router link refer : https://stackoverflow.com/questions/51821766/angular-m ...

  7. Vue.js组件之同级之间的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制

    前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零开始,教给大家vue的基础.高级操作.组件 ...

  9. vue工程利用pubsub-js实现兄弟组件之间的通信

    前言 项目是基于vue-cli创建的,不会搭建vue开发环境的同学可以百度,这里不再赘述. 步骤流程 vue项目搭建完成之后的文件图如下: 我的上一篇博客已经详细叙述vue工程中各个文件的作用,不清楚 ...

随机推荐

  1. linux下vi或vim操作Found a swap file by the name的原因及解决方法

    在linux下用vi或vim打开Test.java文件时 [root@localhost tmp]# vi Test.java出现了如下信息: E325: ATTENTION    Found a s ...

  2. 微信小程序-bug-调用wx.login()无响应的原因和解决方案

    想必,最近有些小程序开发者,在调用wx.login()的时候,在部分IOS上无响应的情况: 补充一点:在测试模式,调试模式,都是OK的,一上正式环境就GG了,百思不得其解啊! 响应结果如下: 在suc ...

  3. 工具 - 怎么看微信h5的源码?

    这个问题在我看网易的h5案例的时候萌生的.因为想看他的源码,但是手机微信打开肯定看不了. 以下几种看代码的方法:(页面案例用网易大大刷屏的h5<二零一六娱乐圈画卷>,真的是一个值得我等众生 ...

  4. PGP NO_PUBKEY

    horizon@horizon-pc ~ $ sudo apt-get update Ign http://packages.linuxmint.com rebecca/upstream Transl ...

  5. GetAsyncKeyState()& 0x8000

    0x8000 & GetKeyState(VK_SHIFT); 这句是判断是否有按下shift键. 关于GetAsyncKeyState与GetKeyState区别:关于GetAsyncKey ...

  6. Listen error 错误和 limit of inotify watches was reached

    今天在生产环境中报错rails c中报了一个错误: FATAL: Listen error: unable to monitor directories for changes. Visit http ...

  7. Java编程基础篇第六章

    构造方法 一:概念: 给对象的数据(属性)进行初始化 二:特点: a.方法名与类同名(字母大小写也要一样) b.没有返回值类型 c.没有具体的返回值 return 三:构造方法重载: 方法名相同,与返 ...

  8. Finalize方法的生成

    Finalize在c#编程语言中需要特殊语法,因此,c#要求在类名前加~符号来定义Finalize方法:例如 internal class FinalizeDemo { ~FinalizeDemo() ...

  9. Tensorflow 的saved_model模块学习

    saved_model模块主要用于TensorFlow Serving.TF Serving是一个将训练好的模型部署至生产环境的系统,主要的优点在于可以保持Server端与API不变的情况下,部署新的 ...

  10. 3、Finished with error: FormatException: Bad UTF-8 encoding 0xc3 (at offset 169)

    这是由于 app 的版本为 release 找不到 keystore 文件, 我们只需要在 app 下的 build.gradle 文件中修改为 signingConfigs.debug 即可: bu ...