组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 datacomputedwatchmethods 以及生命周期钩子等。仅有的例外是像 el 这样根实例特有的选项。

  其中,一个组件的 data 选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝。

data: function () {
return {
count:
}
}

  使用组件为了更好的复用,相当于自定义标签,你可以进行任意次数的复用。

  首先要明确自己组件的功能

  比如实现两个按钮,一个喜欢,一个不喜欢,每次点击可以记录次数。当点击按钮时,每个组件会各自独立维护它的count,每使用一个组件都会有一个新的实例被创建。

// 调用button组件
<hello-world heading="Likes" color="green"></hello-world>
<hello-world heading="Dislikes" color="red"></hello-world>
<template id="button-component">
<div id="box">
<h1>{{ heading }}</h1>
<button @click="count += 1" :class="color">{{ count }}</button>
</div>
</template> <script>
export default {
name: "button-component",
props:['heading','color'],
data:function () { return{
count:0
}
}
}
</script> <style scoped>
button{width: 50px;height: 30px;border-radius: 5px;}
.green{
background:green;
}
.red{
background: red;
}
</style>

出现的问题:对于直接绑定改变style的background值,

style="background: {{ color }}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div style="{{ val }}">, use <div :style="val">.

最新文章

  1. Xamarin.iOS开发初体验
  2. 《TypeScript 中文入门教程》 1、基础数据类型
  3. html5视频播放插件
  4. JavaScript------脚本化HTTP
  5. Swift3.0P1 语法指南——类和结构体
  6. CentOS 6.5下配置iSCSI网络存储
  7. 【node】使用gulp来维护网站项目
  8. python 函数小实例
  9. html插入视频
  10. RedHat7笔记
  11. 【stanford C++】字符串(String)与流(Stream)
  12. IM 融云 之 列表中显示聊天用户名称
  13. 提高java编程质量 - (四)i++ 和 ++i 探究原理
  14. JS声明变量的写法
  15. B2B2C商品模块数据库设计
  16. 新概念英语(1-29)Come in, Amy.
  17. 2019-04-15 Python之利用matplotlib和numpy的简单绘图
  18. STL 小白学习(8) set 二叉树
  19. __enter__ 和 __exit__上下文管理协议
  20. 禁止选择DIV内的文本(css,js写法)

热门文章

  1. ewebeditor上传文件大小
  2. Linux基础学习-使用Squid部署代理缓存服务
  3. perl中foreach(一)
  4. Python三元表达式和列表生成式
  5. 几条sql语句(exists)
  6. magic mouse 2 在Mac上灵敏度太低的解决办法
  7. joyoi1935 「Poetize3」导弹防御塔
  8. Java web 服务启动时Xss溢出异常处理笔记
  9. ppp详解
  10. HDU 3879 Base Station