<!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自學(xué):全局組件和局部組件</title>
</head>
<body>
<div id="app">
<cpn></cpn>
</div>
<div id="app1">
<cpn1></cpn1>
</div>
</body>
<script type="text/javascript">
//注冊組件的步驟解析
//1 Vue.extend():
//調(diào)用Vue.extend()創(chuàng)建的是一個組件構(gòu)造器
//通常在創(chuàng)建組件構(gòu)造器時,傳入template代表我們自定義組件的模板
//該模板就是在使用到組件的地方,顯示為HTML代碼
//事實上,這樣的寫法在Vue2.x的文檔基本看不到,因為一般使用語法糖
//2 Vue.component():
//調(diào)用Vue.component()是將剛才的組件構(gòu)造器注冊為一個組件,并且給它起一個標(biāo)簽名稱
//所以需要傳遞兩個參數(shù):1、組件的名稱 2、組件構(gòu)造器
//注冊了全局組件
//全局組件可以在多個Vue的實例當(dāng)中使用
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>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號