源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Vuejs实现带样式的单文件组件新方法

  • 时间:2021-05-12 23:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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>
*/}))

运行结果: [img]http://files.jb51.net/file_images/article/201705/20175292856693.jpg?2017429297[/img] 以简单的js文件形式实现了Vue单文件组件, 优点是带样式, 用法简单, 接近于.vue文件, 不用webpack, 不用发ajax请求, 直接引入使用 !
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部