<html>
<head>
<title>Vue.js Component</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent></testcomponent>
</div>
<script src="index.js"></script>
</body>
</html>
Vue.component('testcomponent',{
template : '<h1>Hello Students!</h1>'
});
var vm = new Vue({
el: '#component_test'
})
html, body {
margin: 5px;
padding: 0;
}
<div id = "component_test">
<testcomponent></testcomponent>
<testcomponent></testcomponent>
<testcomponent></testcomponent>
<testcomponent></testcomponent>
<testcomponent></testcomponent>
</div>
<html>
<head>
<title>Vue.js Component</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent>Hello Rahul!</testcomponent>
<testcomponent>Hello James!</testcomponent>
<testcomponent>Hello Alex!</testcomponent>
<testcomponent>Hello Priti!</testcomponent>
<testcomponent>Hello Mohan!</testcomponent>
</div>
<script src="index.js"></script>
</body>
</html>
template : '<h1><slot></slot></h1>',
<html>
<head>
<title>Vue.js Component</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent>Hello Rahul!</testcomponent>
<testcomponent>Hello James!</testcomponent>
<testcomponent>Hello Alex!</testcomponent>
<testcomponent>Hello Priti!</testcomponent>
<testcomponent>Hello Mohan!</testcomponent>
</div>
<script src="index.js"></script>
</body>
</html>
Vue.component('testcomponent',{
template : '<h1><slot></slot></h1>',
});
var vm = new Vue({
el: '#component_test'
})
<html>
<head>
<title>Vue.js Component</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "component_test">
<testcomponent :elementtype = "'div,red,25,div1'">Hello Rahul!</testcomponent>
<testcomponent :elementtype = "'h2,brown,25,div1'">Hello James!</testcomponent>
<testcomponent :elementtype = "'p,blue,25,ptag'">Hello Alex!</testcomponent>
<testcomponent :elementtype = "'div,green,25,divtag'">Hello Priti!</testcomponent>
<testcomponent :elementtype = "'h4,blue,25,ptag'">Hello Mohan!</testcomponent>
</div>
<script src="index.js"></script>
</body>
</html>
Vue.component('testcomponent',{
render :function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
props:{
elementtype:{
attributes:String,
required:true
}
}
});
var vm = new Vue({
el: '#component_test'
})
Vue.component('testcomponent',{
render :function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
props:{
elementtype:{
attributes:String,
required:true
}
}
});
props:{
elementtype:{
attributes:String,
required:true
}
}
});
(renderElement: String | Component, definition: Object, children: String | Array) argument
render :function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
render :function(createElement){
var a = this.elementtype.split(",");
return createElement(a[0],{
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
<testcomponent :elementtype = "'div,red,25,div1'">Hello Rahul!</testcomponent>
return createElement(a[0],{
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
this.$slots.default
)
},
attrs:{
id:a[3],
style:"color:"+a[1]+";font-size:"+a[2]+";"
}
},
在id标记中,我们传递了[3],这是在用逗号分割后的值。
在样式标签中,我们分别传递了a [1]和a [2]来定义颜色和字体大小。
<testcomponent :elementtype = "'div,red,25,div1'">Hello Rahul!</testcomponent>
this.$slots.default
<div id = "component_test">
<testcomponent :elementtype = "'div,red,25,div1'">Hello Rahul!</testcomponent>
<testcomponent :elementtype = "'h2,brown,25,div1'">Hello James!</testcomponent>
<testcomponent :elementtype = "'p,blue,25,ptag'">Hello Alex!</testcomponent>
<testcomponent :elementtype = "'div,green,25,divtag'">Hello Priti!</testcomponent>
<testcomponent :elementtype = "'h4,blue,25,ptag'">Hello Mohan!</testcomponent>
</div>
<html>
<head>
<title>Vue.js Event binding in render function</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="render_1"></div>
<script src="index.js"></script>
</body>
</html>
new Vue({
el: '#render_1',
data() {
return {
clickCount: 0,
}
},
methods: {
onClick() {
this.clickCount += 1;
}
},
render(createElement) {
const button = createElement('button', {
on: {
click: this.onClick
}
}, 'Click me');
const counter = createElement('span', [
'Number of clicks:',
this.clickCount
]);
return createElement('div', [
button, counter
])
}
});