欢迎来到博客园~~~

vue.js基础__component 初识组件

组件定义可分为两部分:

一种是全局组件,另一种是局部组件;

区别是全局组件可以全局调用,而局部组件,在作用域外不可调用

1、首先来看一下全局组件,代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>componen-1t</title>
<script src="../assets/js/vue.js"></script>
</head>

<body>
<h1>component-1</h1>
<hr>
<div id="app">
<js></js>
</div>
<script>
Vue.component('js', {
template: `<div style="color:red">我是全局的js组件</div>`
})

var app = new Vue({
el: '#app'
})
</script>
</body>

</html>

 

2、下面是局部组件的定义方法:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>componen-1t</title>
<script src="../assets/js/vue.js"></script>
</head>

<body>
<h1>component-1</h1>
<hr>
<div id="app">
<js></js>
</div>
<script>
var app = new Vue({
el: '#app',
components: {
'js: {
template: `<div style="color:green">我是局部的js组件</div>`
}
}
})
</script>
</body>

</html>

 

 
posted @ 2019-06-28 08:36  孙~洋  阅读(564)  评论(0编辑  收藏  举报