Vue.extend 返回的是一個“擴展實例構造器”,也就是預設了部分選項的Vue實例構造器。經常服務於Vue.component用來生成組件,可以簡單理解為當在模板中遇到該組件名稱作為標簽的自定義元素時,會自動調用“擴展實例構造器”來生產組件實例,並掛載到自定義元素上。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="../assets/js/vue.js"></script>
<title>vue.extend-擴展實例構造器</title>
</head>
<body>
<h1>vue.extend-擴展實例構造器</h1>
<hr>
<author></author>
<script type="text/javascript">
var authorExtend = Vue.extend({
template:"<p><a :href='authorUrl'>{{authorName}}</a></p>",
data:function(){
return{
authorName:'JSPang',
authorUrl:'http://www.jspang.com'
}
}
});
new authorExtend().$mount('author');
</script>
</body>
</html>
還可以通過HTML標簽上的id或者class來生成擴展實例構造器,Vue.extend里的代碼是一樣的,只是在掛載的時候,我們用類似jquery的選擇器的方法,來進行掛載就可以了。
new authorExtend().$mount('#author');
|