Vuejs實現帶樣式的單檔案組件新方法,vuejs新方法

來源:互聯網
上載者:User

Vuejs實現帶樣式的單檔案組件新方法,vuejs新方法

本文執行個體為大家分享了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請求, 直接引入使用 !

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.