-
-
Save lordjoo/4a389ebe0c1fe32b2255db0c5a813fa4 to your computer and use it in GitHub Desktop.
Dynamically generate html element by Vue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Vue.js Simple Example</title> | |
</head> | |
<style> | |
body { | |
font : 100% Helvetica, sans-serif; | |
color: #ecf0f1; | |
text-align: center; | |
background-color: #34495e; | |
} | |
.container { | |
margin: 0 auto; | |
padding-top: 70px; | |
} | |
div { | |
border: 1px solid black; | |
} | |
</style> | |
<body> | |
<div id="app" class="container"> | |
<velement v-for="element in elements" :model="element"></velement> | |
<script type="text/x-template" id="velement-template"> | |
<div> | |
<div class="row" v-if="model.type == 'row'"> | |
<velement v-if="model.elements" v-for="element in model.elements" :model="element"> | |
</velement> | |
</div> | |
<div class="col-12" v-if="model.type == 'col'"> | |
<velement v-if="model.elements" v-for="element in model.elements" :model="element"> | |
</velement> | |
</div> | |
<p v-if="model.type == 'text'"> | |
<velement v-if="model.elements" v-for="element in model.elements" :model="element"> | |
</velement> | |
{{ model.content }} | |
</p> | |
</div> | |
</script> | |
</div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.1/vue.js"></script> | |
<script> | |
Vue.component('velement', { | |
template: '#velement-template', | |
props: ['model'], | |
data: function () { | |
return { | |
element: {} | |
} | |
} | |
}); | |
// create a root instance | |
new Vue({ | |
el: '#app', | |
data: function () { | |
return { | |
model: {}, | |
elements: [ | |
{ | |
type: 'row', | |
elements: [ | |
{ | |
type: 'col', | |
width: 6, | |
elements: [ | |
{ | |
type: 'text', | |
content: 'Hello World!' | |
} | |
] | |
}, | |
] | |
} | |
] | |
} | |
} | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment