vue内置组件component--通过is属性动态渲染组件操作

我就废话不多说了,大家看代码吧~

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.jsdelivr.net/npm/vue"></script>
		<script src="https://cdn.bootcss.com/vue-router/3.1.3/vue-router.js"></script>
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	</head>
	<body>
		<div id="app">
			<input @click="currentrouter='Home'" type="button" value="首页"/>
			<input @click="currentrouter='Fenlei'" type="button" value="分类"/>
			<input @click="currentrouter='My'" type="button" value="我的"/>
			<!-- 动态组件 component -->
			<component :is="currentrouter"></component>
		</div>
		
		<template id="home">
			<div>
				{{msg}}
			</div>
		</template>
		<template id="fenlei">
			<div>
				{{msg}}
			</div>
		</template>
		<template id="my">
			<div>
				{{msg}}
			</div>
		</template>
		
		<script>
			//局部定义三个组件
			const Home = {
				template:"#home",
				data(){
					return{
						msg:"这里是home组件"
					}
				}
			}
			const Fenlei = {
				template:"#fenlei",
				data(){
					return{
						msg:"这里是fenlei组件"
					}
				}
			}
			const My = {
				template:"#my",
				data(){
					return{
						msg:"这里是my组件"
					}
				},
			}
			//vue 实例
			var vm = new Vue({
				el:"#app",
				components:{
					Home,
					Fenlei,
					My
				},
				data:{
					msg:"hello world",
					currentrouter:"Home"
				},
				methods:{
					
				}
			})
			
		</script>
	</body>
</html>

补充知识:详解vue组件的is特性:限制元素&动态组件

在vue.js组件教程的一开始提及到了is特性

意思就是有些元素,比如 ul 里面只能直接包含 li元素,像这样:

<ul>
  <li></li>
</ul>
//而不能:
<ul>
  <your-component>
</ul>

这样就不能复用your-component这个组件了,如果要达到我们的目的,我们就要使用is特性像这样:

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

组件功能是vue项目的一大特色。组件可以扩展html元素,可以封装可重用的代码,可以增加开发效率。它是自定义元素,vue.js的编译器为它添加特殊功能。有些情况,组件也可以是原生HTML元素的形式,以is特性进行扩展。

那么is特性究竟是什么呢?有什么用途呢?

1、限制元素

其实简单的来说,因为vue模板就是dom模板,使用的是浏览器原生的解析器进行解析,所以dom模板的限制也就成为vue模板的限制了,要求vue模板是有效的HTML代码片段。但是由于dom的一些html元素对放入它里面的元素有限制,所以导致有些组件没办法放在一些标签中,比如<ul></ul> <select></select><a></a> <table></table>等等这些标签中,所以需要增加is特性来扩展,从而达到可以在这些受限制的html元素中使用。例如:

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

而不能使用下面的方式,因为下面的方式会将自定义组件<my-component>当做无效的内容,导致错误的渲染结果

<ul>
 <my-component></mu-component>
<ul>

其实两种写法表达的意思是一致,但是第二种写法是不合法的,会导致错误。

2、动态组件

在我们平时使用vue中的模板的时候,许多时候都是直接定义成一个固定的模板,但是,vue中提供了一个动态模板,可以在任意模板中切换,就是用vue中<component>用:is来挂载不同的组件。

<div id="app" v-cloak>
    <component :is="currentView"></component>
    <button @click="handleChangeView('A')">A</button>
    <button @click="handleChangeView('B')">B</button>
    <button @click="handleChangeView('C')">C</button>
</div>

    var app = new Vue({
      el: '#app',
      components:{
        comA:{
          template:`
            <div>组件A</div>
          `
        },
        comB:{
          template:`
            <div>组件B</div>
          `
        },
        comC:{
          template:`
            <div>组件C</div>
          `
        }
      },
      data:{
        currentView:'comA'
      },
      methods:{
        handleChangeView:function(component){
          this.currentView='com'+component;
        }
      }
    });

我们在components中注册了三个模板,当我们点击当前按钮的时候,就会将模板切换模板,可以说是非常方便了。

以上这篇vue内置组件component--通过is属性动态渲染组件操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。