本文實例為大家分享了Vuejs實現單文件組件的方法,供大家參考,具體內容如下
代碼如下:
example.html
<script src="vue.js"></script><div id="example"> <h3>Vue component<h3> <counter></counter> <counter></counter></div>//引入組件mycomp.js<script src="mycomp.js"></script><script>new Vue({ el: '#example'})</script>mycomp.js
//heredoc方法輸出注釋中的組件代碼function heredoc(fn){ return fn.toString().match(/[^]*///*([^]*)/*///}$/)[1];}//輸出組件代碼document.write(heredoc(function(){/*<style>.my {color:red;padding:10px;}</style><script type="text/x-template" id="c"><p class="my" v-on:click="todo+=1"> {{todo}}</p></script><script>Vue.component('counter',{ template: "#c", data: function () { return { todo: 1 } }})</script>*/}))運行結果:

以簡單的js文件形式實現了Vue單文件組件, 優點是帶樣式, 用法簡單, 接近于.vue文件,
不用webpack, 不用發ajax請求, 直接引入使用 !
|
新聞熱點
疑難解答