【Html】Vue动态插入组件
html:
<div id="app">
<p>{{ message }}</p>
<button @click="add('a-component', '我是A')">添加A组件</button>
<button @click="add('b-component', '我是B')">添加B组件</button>
<component :is="item.component" :text="item.text" v-for="item in items"></component>
</div>
js:
const aComponent = Vue.extend({
props: ['text'],
template: '<li>A Component: {{ text }}</li>'
})
const bComponent = Vue.extend({
props: ['text'],
template: '<li>B Component: {{ text }}</li>'
})
new Vue({
el: '#app',
data () {
return {
message: 'Hello Vue.js!',
items: []
}
},
methods: {
add (name, text) {
this.items.push({
component: name,
text: text
})
}
},
components: {
aComponent,
bComponent
}
})
重点是使用了:
Vue.extend
extend 是构造一个组件的语法器.
你给它参数它给你一个组件 然后这个组件
你可以作用到Vue.component 这个全局注册方法里, 也可以在任意vue模板里使用<apple>组件
var apple = Vue.extend({
....
})
Vue.component('apple',apple)
也可以作用到vue实例或者某个组件中的components属性中并在内部使用apple组件
new Vue({
components:{
apple:apple
}
})
查看实例:
https://codepen.io/kakarrot2009/pen/VxLOrQ
以下是其他方法(没有试过):参考https://www.cnblogs.com/h2zZhou/p/8036953.html
方法一、
<template>
<input type="text" v-model='componentName'>
<button @click='add'>click me to add a component</button>
</template> <script>
// 引入要添加的所有组件
import component1 from './components/component1.vue'
import component2 from './components/component2.vue'
export default {
data: function() {
return {
allComponents: [],
componentName: ''
}
},
components: {
// 注册所有组件
component1,
component2
}
methods: {
add: function() {
this.allComponents.push(this.componentName)
// 重置输入框
this.componentName = ''
},
render: function(h) { // h 为 createElement 函数,接受三个参数
// tag
// data
// children 具体看文档吧
return h('div',this.allComponents.map(function(componentName) {
return h(componentName)
}))
}
}
}
</script>
方法二、
html <div id="app">
<button @click="add('a-component', 'test')">Add A</button>
<button @click="add('b-component', 'test')">Add B</button>
<ul>
<li :is="item.component" :text="item.text" v-for="item in items"></li>
</ul>
</div>
javascript var AComponent = Vue.extend({
props: ['text'],
template: '<li>A Component: {{ text }}</li>'
}) var BComponent = Vue.extend({
props: ['text'],
template: '<li>B Component: {{ text }}</li>'
}) new Vue({
el: '#app',
components: {
'a-component': AComponent,
'b-component': BComponent,
},
data: {
items: []
},
methods: {
add(component, text) {
this.items.push({
'component': component,
'text': text,
})
}
}
})
方法三、
//我是写在父组件中的:
Vue.component('mycontent', {
props: ['content'],
data: function() {
return {
coms: [],
}
},
render: function(h) {
this.coms = [];
for(var i = 0; i < this.content.length; i++) {
this.coms.push(h(this.content[i], {}))
}
return h('div', {},
this.coms)
},
});
//调用的时候
<mycontent v-bind:content="content"></mycontent>
//那么父组件中的content变化时,就会动态加载组件了
【Html】Vue动态插入组件的更多相关文章
- vue 动态插入组件
HTML代码: <div id="app"> <p>{{ message }}</p> <button @click="add( ...
- Vue动态创建组件方法
组件写好之后有的时候需要动态创建组件.例如: 编辑文章页面,正文是一个富文本编辑器,富文本编辑器是一个第三方的组件,点击添加章节的时候需要动态的创建一个富文本编辑器这个时候怎么处理呢. 富文本编辑器也 ...
- vue动态子组件的实现方式
让多个组件使用同一个挂载点,并动态切换,这就是动态组件. 通过使用保留的 <component>元素,动态地绑定到它的 is 特性,可以实现动态组件. 方式一:局部注册所需组件 <d ...
- ZUI(BootStrap)使用vue动态插入HTMl所创建的data-toggle事件初始化方法
用ZUI的图片浏览:lightbox 写静态html的时候是有预览效果的,使用了vue动态加载就没有效果了, 网上的说法是动态生成的没有激活事件:ZUI(BootStrap)动态插入HTMl所创建的d ...
- 第八十七篇:Vue动态切换组件的展示和隐藏
好家伙, 1.什么是动态组件? 动态组件指的是动态切换组件的限制与隐藏 2.如何实现动态组件渲染 vue提供了一个内置的<component>组件,专门用来实现动态组件的渲染. 可以将其看 ...
- vue动态切换组件
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- vue动态生成组件
单个组件引用,引入此文件js.全局使用,注册到vue的main文件Vue.prototype.create = Create create.js import Vue from 'vue';impor ...
- vue 动态创建组件(运行时创建组件)
function mountCmp (cmp, props, parent) { if (cmp.default) { cmp = cmp.default } cmp = Vue.extend(cmp ...
- Vue动态组件
前面的话 让多个组件使用同一个挂载点,并动态切换,这就是动态组件.本文将详细介绍Vue动态组件 概述 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动 ...
随机推荐
- Python | 一行命令生成动态二维码
当我看到别人的二维码都做的这么炫酷的时候,我心动了! 我也想要一个能够吸引眼球的二维码,今天就带大家一起用 Python 来做一个炫酷的二维码! 首先要安装工具 myqr: pip install m ...
- 构造函数 (C++)
构造函数是一种可初始化其类的实例的成员函数. 构造函数具有与类相同的名称,没有返回值. 构造函数可以具有任意数量的参数,类可以具有任意数量的重载构造函数. 构造函数可以具有任何可访问性(公共.受保护或 ...
- 【ARM】AD转换器
A/D转换器 A/D转换器,又称模/数转换器,顾名思义,就是把模拟信号数字化. 由于系统的实际处理对象往往都是一些模拟量(如温度.压力.位移.图像等),要使计算机或数字仪表能识别和处理这些信号,必须首 ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- S3C2440时钟体系结构
任意一款单板,我们了解其时钟都是通过时钟树来的. 这里没有全部截完,只是讲解时钟来源,OSC代表晶振,这说明我们的时钟可以来至晶振OSC也可以来至外部输入EXTCLK,这是通过OM选择器来完成的. 2 ...
- Spring事务管理要点总结
# Spring事务管理要点总结 ### 要点---- 事务是企业级应用中必不可缺少的技术,用来确保数据的完整性和一致性.- Spring事务管理并不实现事务管理的实现,而是借助Hibernate\J ...
- postgresql 修改字段名称
ALTER TABLE auth_user RENAME email TO aemail;
- jdbc连接sqlserver报错java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
使用2008的数据库, 我已经引入的sqljdbc4的包,单还是报这个错,很奇怪突然想到在配置hibernate的时候,是拷贝下来的代码 然后到网上查了下, 因为是2008的版本驱动和2000的有点不 ...
- MFC工程说明readme
======================================================================== MICROSOFT FOUNDATION CLASS ...