vue内置component组件 v-bind: is:实现动态组件

2020-03-11 09:39:03

 

参考地址 内置组件 + v-bind: is:实现动态组件

<component> 元素是vue 里面的一个内置组件。


在<component>里面使用 v-bind: is,可以实现动态组件的效果。






例子解析:

下面例子创建一个包含多个子组件的 vue 实例。


1. vue代码部分:新建 vue 实例 "app",这个实例的 components 里面,有3个组件,这些组件都有各自的模板。分别是 acomp,bcomp,ccomp


2. html 代码部分:使用vue 的内置组件 <component></component>,并使用 "is" 特性(需要通过v-bind 给 "is" 绑定一个值)。"is" 绑定的值传入一个组件名,就会切换到这个组件。


<div id="app">

<component v-bind:is="whichcomp"></component>

<button v-on:click="choosencomp('a')">a</button>

<button v-on:click="choosencomp('b')">b</button>

<button v-on:click="choosencomp('c')">c</button>

</div>

//做一个包含列表组件

//需要给组件创建props--"todos",用于存放组件通过绑定prop --"todo"获取实例中的data数据"todolists"

 

var app=new Vue({

  el: '#app',

components:{

acomp:{

   template:`

<p>这里是组件A</p>

`

},

bcomp:{

   template:`

<p>这里是组件B</p> `

},

ccomp:{

template:`

<p>这里是组件C</p>

`

}},

data:{whichcomp:""},

methods:{

   choosencomp:function(x){

   this.whichcomp=x+"comp"}

   }

})

网页渲染效果:

点击 A 按钮,文字显示切换到 "显示组件A" 




点击C 按钮,文字显示切换到 "显示组件C"


  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).

  • 2017-02-16 08:09:01

    HTML中PRE和p的区别

    pre 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。 <pre> 标签的一个常见应用就是用来表示计算机的源代码。

  • 2017-02-16 15:14:14

    动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法。