Vue2.0 - 構造器的延伸 Vue.extend

Vue.extend 返回的是一個「擴展實例構造器」,也就是預設了部分選項的Vue實例構造器。常常服務於Vue.component用來生成組件,能夠簡單理解爲當在模板中遇到該組件名稱做爲標籤的自定義元素時,會自動調用「擴展實例構造器」來生產組件實例,並掛載到自定義元素上。javascript

<!DOCTYPE html>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');
相關文章
相關標籤/搜索