全局组件
<html>
<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>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<body>
<div id="app">
<mycomponent></mycomponent>
</div>
<script>
Vue.component('mycomponent',{
template: `<div>这是一个全局定义组件</div>`,
data(){
return{
message: 'hello world'
}
}
})
new Vue({
el: '#app',
})
</script>
</body>
</html>
渲染至页面的html结构:
局部组件
<html>
<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>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<body>
<div id="app">
<my-component></my-component>
</div>
<script>
var Child = {
template: `<div>我是自定义局部组件</div>`
}
new Vue({
el:'#app',
components:{
'my-component': Child
}
})
</script>
</body>
</html>
渲染至页面的html结构: