【Vue】is特性

1.限制元素

Vue模板就是dom模板,而dom对一些html元素是有限制的,比如

<table>
	<my-component></my-component>
</table>

这样是不行的
通过is我们可以拓展html元素,比如

<ul>
  <li is="my-component"></li>
</ul>

2.动态组件

通过is我们可以动态的更换模板中的组件

<div id="app">
    <component :is="currentView"></component>
    <button @click="handleChangeComponent('component-one')">A</button>
    <button @click="handleChangeComponent('component-two')">B</button>
</div>
 var app = new Vue({
     el: '#app',
     components:{
       component-one:{
         template:`
           <div>组件一</div>
            `
       },
       component-two:{
          template:`
            <div>组件二</div>
             `
       },
     },
     data:{
        currentView:'component-one'
     },
     methods:{
        handleChangeComponent:function(component){
           this.currentView = component;
        }
     }
});

猜你喜欢

转载自blog.csdn.net/jzq950522/article/details/88411121