Blogger Information
Blog 250
fans 3
comment 0
visits 321148
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue自学:全局组件和局部组件
梁凯达的博客
Original
1355 people have browsed it

<!DOCTYPE html>

<html lang="zh">
<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">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
<title>Vue自学:全局组件和局部组件</title>
</head>
<body>
<div id="app">
<cpn></cpn>
</div>
<div id="app1">
<cpn1></cpn1>
</div>
</body>
<script type="text/javascript">
//注册组件的步骤解析
//1 Vue.extend():
//调用Vue.extend()创建的是一个组件构造器
//通常在创建组件构造器时,传入template代表我们自定义组件的模板
//该模板就是在使用到组件的地方,显示为HTML代码
//事实上,这样的写法在Vue2.x的文档基本看不到,因为一般使用语法糖
//2 Vue.component():
//调用Vue.component()是将刚才的组件构造器注册为一个组件,并且给它起一个标签名称
//所以需要传递两个参数:1、组件的名称 2、组件构造器
//注册了全局组件
//全局组件可以在多个Vue的实例当中使用
Vue.component(‘cpn’,{
template:
<div id="main"> <h2>测试div1</h2> <h3>测试div2</h3> </div>
})
const app = new Vue({
el:’#app’,
data:{

}
})
//
const app1 = new Vue({
el:’#app1’,
data:{

},
components:{
cpn:cpn1,
template:
<div id="main"> <h2>局部组件</h2> <h3>局部组件</h3> </div>
}
})
</script>
</html>

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post