封装了一个my-li 只要引用 页面就会展示hello
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<ul>
<my-li></my-li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script type="text/javascript">
Vue.component("my-li",{
template: "<li>Hello</li>"
})
var vm = new Vue({
el: "#app",
data: {
message: "Hello Vue"
}
});
</script>
</body>
</html>
还可以通过数据绑定的方式展示一个数组
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<ul>
<my-li v-for="name in names" v-bind:name="name"></my-li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script type="text/javascript">
Vue.component("my-li",{
props: ['name'],
template: "<li>{{name}}</li>"
})
var vm = new Vue({
el: "#app",
data: {
names: ["1","2","3","4"]
}
});
</script>
</body>
</html>