一、组件

  • 组件(Conponent)是Vue.js最强大的功能
  • 组件可以扩展HTML元素,封装可重用的代码

1.1、组件注册

全局注册

  • Vue.component( ‘组件名称’ , { } ) 第一个参数是标签名称, 第二个参数是一个可选对象
  • 全局组件注册后,任何Vue的实例都可以使用

组件基础使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <my-component></my-component>
  11. </div>
  12. <script>
  13. Vue.component('my-component', {
  14. data () { // 局部作用域, 作用域和变量都是独立的
  15. return {
  16. msg: "HelloWorld"
  17. }
  18. },
  19. template: '<div> A Custom Component! {{items}}</div>'
  20. })
  21. new Vue({
  22. el: '#app',
  23. data: {
  24. msg: '12344'
  25. }
  26. })
  27. </script>
  28. </body>
  29. </html>

局部注册

  • 只能在当前的Vue实例中使用

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    7. </head>
    8. <body>
    9. <div id="app">
    10. <my-component></my-component>
    11. </div>
    12. <script>
    13. new Vue({
    14. el: '#app',
    15. data: {
    16. msg: '12344'
    17. },
    18. components: {
    19. 'my-component': {
    20. template: '<div>A Custom Component!</div>'
    21. }
    22. }
    23. })
    24. </script>
    25. </body>
    26. </html>

    二、Vue组件之间传值

    2.1、父组件向子组件传值

  • 父组件发送的形式是以属性的形式绑定到子组件身上

  • 然后子组件使用怕props接收
  • 在props中使用驼峰形式,模板中需要使用短横线的形式的字符串 模板中没有这个限制

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    7. </head>
    8. <body>
    9. <div id="app">
    10. <div>{{pMsg}}</div>
    11. <menu-item :title="pTitle" content="Hello"></menu-item>
    12. </div>
    13. <script>
    14. Vue.component("menu-item", {
    15. props: ['title', 'content'],
    16. data () {
    17. return {
    18. msg: '子组件自身的内容'
    19. }
    20. },
    21. template: '<div>{{msg + "---" + title + "----" + content}}</div>'
    22. })
    23. new Vue({
    24. el: '#app',
    25. data: {
    26. pMsg: '父组件中的内容',
    27. pTitle: '动态绑定属性'
    28. }
    29. })
    30. </script>
    31. </body>
    32. </html>

    2.2、子组件向父组件传值

  • 子组件使用 $emit() 触发事件

  • $emit()第一个参数为自定义的事件名称 第二个参数为需要传递的数据
  • 父组件用 v-on 监听子组件的事件
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    7. </head>
    8. <body>
    9. <div id="app">
    10. <div :style="{fontSize: fontSize + 'px'}">{{pMsg}}</div>
    11. <menu-item :arr="pArray" @enlarge-text="handle($event)"></menu-item>
    12. </div>
    13. <script>
    14. Vue.component("menu-item", {
    15. props: ['arr'],
    16. template: `
    17. <div>
    18. <ul>
    19. <li :key="index" v-for="(item, index) in arr">{{ item }}</li>
    20. </ul>
    21. <button @click="$emit('enlarge-text', 5)">扩大父组件中的字体大小</button>
    22. <button @click="$emit('enlarge-text', 10)">扩大父组件中的字体大小</button>
    23. </div>`
    24. })
    25. new Vue({
    26. el: "#app",
    27. data: {
    28. pMsg: '父组件中的内容',
    29. pArray: ['apple', 'orange', 'banana'],
    30. fontSize: 12
    31. },
    32. methods:{
    33. handle (val) {
    34. this.fontSize += val
    35. }
    36. }
    37. })
    38. </script>
    39. </body>
    40. </html>

2.3、兄弟之间的传递

  • 兄弟之间传递数据需要借助事件中心,通过事件中心传递数据
    • 提供事件中心 let hub = new Vue()
  • 传递数据方, 通过 hub.$emit(方法名,传递的数据)
  • 接收数据方,通过 mounted() {} 钩子函数中 触发 hub.$on(方法名)
  • 销毁事件, 通过 hub.$off(方法名) 销毁之后无法进行数据传递
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <div>父组件</div>
  11. <div>
  12. <button @click="handle">销毁事件</button>
  13. </div>
  14. <test-tom></test-tom>
  15. <test-jerry></test-jerry>
  16. </div>
  17. <script>
  18. // 提供一个事件中心
  19. let hub = new Vue();
  20. Vue.component('test-tom', {
  21. data: function () {
  22. return {
  23. num: 0
  24. }
  25. },
  26. template: `
  27. <div>
  28. <div>TOM:{{ num }}</div>
  29. <div>
  30. <button @click='handle'>点击</button>
  31. </div>
  32. </div>`,
  33. methods: {
  34. handle: function () {
  35. //2、传递数据方,通过一个事件触发hub.$emit(方法名,传递的数据) 触发兄弟组件的事件
  36. hub.$emit('jerry-event', 2);
  37. }
  38. },
  39. mounted: function () {
  40. // 3、接收数据方,通过mounted(){} 钩子中 触发hub.$on(方法名
  41. hub.$on('tom-event', (val) => {
  42. this.num += val;
  43. });
  44. }
  45. });
  46. Vue.component('test-jerry', {
  47. data: function () {
  48. return {
  49. num: 0
  50. }
  51. },
  52. template: `
  53. <div>
  54. <div>JERRY:{{ num }}</div>
  55. <div>
  56. <button @click='handle'>点击</button>
  57. </div>
  58. </div>
  59. `,
  60. methods: {
  61. handle: function () {
  62. //2、传递数据方,通过一个事件触发hub.$emit(方法名,传递的数据) 触发兄弟组件的事件
  63. hub.$emit('tom-event', 1);
  64. }
  65. },
  66. mounted: function () {
  67. // 3、接收数据方,通过mounted(){} 钩子中 触发hub.$on()方法名
  68. hub.$on('jerry-event', (val) => {
  69. this.num += val;
  70. });
  71. }
  72. });
  73. new Vue({
  74. el: '#app',
  75. methods: {
  76. handle() {
  77. hub.$off("tom-event")
  78. hub.$off("jerry-event")
  79. }
  80. }
  81. })
  82. </script>
  83. </body>
  84. </html>

三、组件插槽

  • 组件最大的特性其实就是它的复用性,而用好组件插槽能更好的底稿组件的可复用性。

    3.1、匿名插槽

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    7. </head>
    8. <body>
    9. <div id="app">
    10. <alert-box>有Bug发生</alert-box>
    11. <alert-box>有一个警告</alert-box>
    12. <alert-box></alert-box>
    13. </div>
    14. <script>
    15. Vue.component('alert-box', {
    16. template: `
    17. <div>
    18. <strong>Error:</strong>
    19. <slot>默认内容</slot>
    20. </div>
    21. `
    22. })
    23. new Vue({
    24. el: '#app',
    25. data() {
    26. return {}
    27. }
    28. })
    29. </script>
    30. </body>
    31. </html>

    3.2、具名插槽

  • 具有名字的插槽

  • 使用 name 属性来绑定元素
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <base-layout>
  11. <p slot="header">标题信息</p>
  12. <p>主要内容一</p>
  13. <p>主要内容二</p>
  14. <p slot="footer">底部信息</p>
  15. </base-layout>
  16. <base-layout>
  17. <!-- 注意点:template 临时包裹标签最终不会渲染到页面上 -->
  18. <template slot="header">
  19. 标题信息1
  20. </template>
  21. <p>主要内容一</p>
  22. <p>主要内容一</p>
  23. <template slot="footer">
  24. <p>底部信息一</p>
  25. </template>
  26. </base-layout>
  27. </div>
  28. <script>
  29. Vue.component('base-layout', {
  30. template: `
  31. <div>
  32. <header><slot name="header"></slot></header>
  33. <main><slot></slot></main>
  34. <footer><slot name="footer"></slot></footer>
  35. </div>
  36. `
  37. })
  38. new Vue({
  39. el: '#app',
  40. data() {
  41. return {}
  42. }
  43. })
  44. </script>
  45. </body>
  46. </html>

3.3、作用域插槽

  • 父组件对子组件加工处理
  • 既可以复用父组件的slot,又可以使slot内容不一致
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <fruit-list :list="list">
  11. <template slot-scope="slotProps">
  12. <strong v-if="slotProps.info.id == 3" class=" current">
  13. {{slotProps.info.name}}
  14. </strong>
  15. <span v-else>{{slotProps.info.name}}</span>
  16. </template>
  17. </fruit-list>
  18. </div>
  19. <script>
  20. Vue.component("fruit-list", {
  21. props: ['list'],
  22. template: `<div>
  23. <li v-for="item in list" :key="item.id">
  24. <slot :info="item">{{item.name}}}</slot>
  25. </li>
  26. </div>`
  27. })
  28. new Vue({
  29. el: '#app',
  30. data: {
  31. list: [
  32. {
  33. id: 1,
  34. name: 'apple'
  35. },
  36. {
  37. id: 2,
  38. name: 'orange'
  39. },
  40. {
  41. id: 3,
  42. name: 'banana'
  43. }
  44. ]
  45. }
  46. })
  47. </script>
  48. </body>
  49. </html>

四、购物车案例

  1. <!doctype html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Document</title>
  9. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  10. <style>
  11. /* 公共样式 */
  12. * {
  13. margin: 0;
  14. padding: 0;
  15. }
  16. html, body {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. li {
  21. list-style-type: none;
  22. }
  23. a {
  24. color: black !important;
  25. }
  26. .layout {
  27. width: 1226px;
  28. margin: 0 auto;
  29. }
  30. .fl {
  31. float: left;
  32. }
  33. .fr {
  34. float: right;
  35. }
  36. /* 头部样式 */
  37. .header {
  38. width: 1226px;
  39. height: 43px;
  40. background-color: #f3f3f3;
  41. border: 1px solid #e9e9e9;
  42. margin: 50px auto 0;
  43. }
  44. .header .layout {
  45. height: 100%;
  46. /*border: 1px solid red;*/
  47. }
  48. .header ul {
  49. display: flex;
  50. }
  51. /* 主体样式 */
  52. main {
  53. width: 1226px;
  54. height: 119px;
  55. margin: 0 auto;
  56. border: 1px solid #c5c5c5;
  57. border-top: none;
  58. display: flex;
  59. justify-content: flex-start;
  60. align-items: center;
  61. font-size: 12px;
  62. }
  63. main > input:first-child {
  64. margin-left: 10px;
  65. }
  66. main > img {
  67. margin-left: 10px;
  68. }
  69. main > .content {
  70. / / width: 200 px;
  71. margin-left: 20px;
  72. font-size: 12px;
  73. padding-right: 300px;
  74. }
  75. main > .price {
  76. padding-bottom: 20px;
  77. }
  78. main > .count {
  79. width: 160px;
  80. height: 20px;
  81. margin-left: 30px;
  82. display: flex;
  83. justify-content: center;
  84. align-items: center;
  85. flex-direction: column;
  86. }
  87. main > .count > .countNum > input[type=button] {
  88. display: block;
  89. width: 20px;
  90. height: 20px;
  91. float: left;
  92. outline: none;
  93. }
  94. main > .count > .countNum > input[type=text] {
  95. width: 40px;
  96. height: 18px;
  97. outline: none;
  98. line-height: 18px;
  99. font-size: 12px;
  100. text-align: center;
  101. float: left;
  102. border-radius: 0;
  103. border-width: 1px;
  104. border-left: none;
  105. border-right: none;
  106. }
  107. main > .total {
  108. width: 80px;
  109. padding-bottom: 20px;
  110. }
  111. main > .edit {
  112. margin-left: 30px;
  113. display: flex;
  114. justify-content: center;
  115. align-items: center;
  116. flex-direction: column;
  117. }
  118. /* 尾部样式 */
  119. .footer {
  120. width: 1226px;
  121. height: 48px;
  122. border: 1px solid #eee;
  123. margin: 10px auto 0;
  124. }
  125. .footer .layout {
  126. height: 100%;
  127. }
  128. .footer .layout > ul {
  129. display: flex;
  130. justify-content: space-between;
  131. align-items: center;
  132. }
  133. </style>
  134. <script>
  135. function changeTwoDecimal_f (x) {
  136. var f_x = parseFloat(x);
  137. if (isNaN(f_x)) {
  138. return 0;
  139. }
  140. var f_x = Math.round(x * 100) / 100;
  141. var s_x = f_x.toString();
  142. var pos_decimal = s_x.indexOf('.');
  143. if (pos_decimal < 0) {
  144. pos_decimal = s_x.length;
  145. s_x += '.';
  146. }
  147. while (s_x.length <= pos_decimal + 2) {
  148. s_x += '0';
  149. }
  150. return s_x;
  151. }
  152. </script>
  153. </head>
  154. <body>
  155. <div class="container">
  156. <cart-header :list="headerList"></cart-header>
  157. <cart-main
  158. v-for="item in contentList"
  159. :key="item.id"
  160. :list="item"
  161. @reduce="reduce($event)"
  162. @add="add($event)"
  163. @handle="handle($event)"
  164. ></cart-main>
  165. <cart-footer :list="footerList" :allPrice="allPrice"></cart-footer>
  166. </div>
  167. <script>
  168. new Vue({
  169. el: '.container',
  170. data () {
  171. return {
  172. headerList: [
  173. {
  174. text: '全选',
  175. style: {
  176. width: '133px',
  177. height: '43px',
  178. display: 'flex',
  179. justifyContent: 'flex-start',
  180. alignItems: 'center',
  181. paddingLeft: '10px',
  182. boxSizing: 'border-box'
  183. },
  184. childrenStyle: {
  185. marginLeft: '10px',
  186. fontSize: '12px'
  187. },
  188. inputShow: true,
  189. checked: false
  190. },
  191. {
  192. text: '商品',
  193. style: {
  194. width: '580px',
  195. height: '43px',
  196. display: 'flex',
  197. justifyContent: 'flex-start',
  198. alignItems: 'center'
  199. },
  200. childrenStyle: {
  201. fontSize: '12px'
  202. },
  203. },
  204. {
  205. text: '单价',
  206. style: {
  207. width: '140px',
  208. height: '43px',
  209. display: 'flex',
  210. justifyContent: 'center',
  211. alignItems: 'center'
  212. },
  213. childrenStyle: {
  214. fontSize: '12px'
  215. },
  216. },
  217. {
  218. text: '数量',
  219. style: {
  220. width: '100px',
  221. height: '43px',
  222. display: 'flex',
  223. justifyContent: 'center',
  224. alignItems: 'center'
  225. },
  226. childrenStyle: {
  227. fontSize: '12px'
  228. },
  229. },
  230. {
  231. text: '小计',
  232. style: {
  233. width: '140px',
  234. height: '43px',
  235. display: 'flex',
  236. justifyContent: 'center',
  237. alignItems: 'center'
  238. },
  239. childrenStyle: {
  240. fontSize: '12px'
  241. },
  242. },
  243. {
  244. text: '操作',
  245. style: {
  246. width: '100px',
  247. height: '43px',
  248. display: 'flex',
  249. justifyContent: 'center',
  250. alignItems: 'center'
  251. },
  252. childrenStyle: {
  253. fontSize: '12px'
  254. },
  255. },
  256. ],
  257. contentList: [
  258. {
  259. id: 1,
  260. inputShow: true,
  261. checked: false,
  262. imgUrl: '//img20.360buyimg.com/n0/s80x80_jfs/t1/87742/10/14484/377198/5e6612e5Ee65f12c5/735fe81565ec86ab.jpg.dpg',
  263. content: '美商海盗船(USCORSAIR)DDR4 3000 32GB(16G×2)套装 台式机内',
  264. price: "959.76",
  265. count: "0",
  266. stock: false, // 有货为 false, 无货为 true
  267. totalCount: "0.00",
  268. edit: [
  269. {
  270. show: true,
  271. type: 1,
  272. text: '删除'
  273. },
  274. {
  275. show: true,
  276. type: 2,
  277. text: '到货通知'
  278. },
  279. {
  280. show: true,
  281. type: 3,
  282. text: '移入关注'
  283. }
  284. ]
  285. },
  286. {
  287. id: 2,
  288. inputShow: true,
  289. checked: false,
  290. imgUrl: '//img20.360buyimg.com/n0/s80x80_jfs/t1/87742/10/14484/377198/5e6612e5Ee65f12c5/735fe81565ec86ab.jpg.dpg',
  291. content: '美商海盗船(USCORSAIR)DDR4 3000 32GB(16G×2)套装 台式机内',
  292. price: "959.00",
  293. count: "0",
  294. stock: false, // 有货为 false, 无货为 true
  295. totalCount: "0.00",
  296. edit: [
  297. {
  298. show: true,
  299. type: 1,
  300. text: '删除'
  301. },
  302. {
  303. show: false,
  304. type: 2,
  305. text: '到货通知'
  306. },
  307. {
  308. show: true,
  309. type: 3,
  310. text: '移入关注'
  311. }
  312. ]
  313. },
  314. {
  315. id: 3,
  316. inputShow: true,
  317. checked: false,
  318. imgUrl: '//img20.360buyimg.com/n0/s80x80_jfs/t1/87742/10/14484/377198/5e6612e5Ee65f12c5/735fe81565ec86ab.jpg.dpg',
  319. content: '美商海盗船(USCORSAIR)DDR4 3000 32GB(16G×2)套装 台式机内',
  320. price: "959.00",
  321. count: "0",
  322. stock: false, // 有货为 false, 无货为 true
  323. totalCount: "0.00",
  324. edit: [
  325. {
  326. show: true,
  327. type: 1,
  328. text: '删除'
  329. },
  330. {
  331. show: false,
  332. type: 2,
  333. text: '到货通知'
  334. },
  335. {
  336. show: true,
  337. type: 3,
  338. text: '移入关注'
  339. }
  340. ]
  341. },
  342. {
  343. id: 4,
  344. inputShow: true,
  345. checked: false,
  346. imgUrl: '//img20.360buyimg.com/n0/s80x80_jfs/t1/87742/10/14484/377198/5e6612e5Ee65f12c5/735fe81565ec86ab.jpg.dpg',
  347. content: '美商海盗船(USCORSAIR)DDR4 3000 32GB(16G×2)套装 台式机内',
  348. price: "959.00",
  349. count: "0",
  350. stock: true, // 有货为 true, 无货为 false
  351. totalCount: "0.00",
  352. edit: [
  353. {
  354. show: true,
  355. type: 1,
  356. text: '删除'
  357. },
  358. {
  359. show: false,
  360. type: 2,
  361. text: '到货通知'
  362. },
  363. {
  364. show: true,
  365. type: 3,
  366. text: '移入关注'
  367. }
  368. ]
  369. },
  370. ],
  371. footerList: [
  372. {
  373. text: '全选',
  374. style: {
  375. width: '900px',
  376. height: '43px',
  377. display: 'flex',
  378. justifyContent: 'flex-start',
  379. alignItems: 'center',
  380. paddingLeft: '10px',
  381. boxSizing: 'border-box'
  382. },
  383. childrenStyle: {
  384. marginLeft: '10px',
  385. fontSize: '12px'
  386. },
  387. inputShow: true,
  388. checked: false
  389. },
  390. {
  391. text: '总价:',
  392. priceShow: true,
  393. style: {
  394. width: '200px',
  395. height: '43px',
  396. display: 'flex',
  397. justifyContent: 'flex-start',
  398. alignItems: 'center',
  399. paddingLeft: '10px',
  400. boxSizing: 'border-box'
  401. },
  402. childrenStyle: {
  403. marginLeft: '10px',
  404. fontSize: '12px'
  405. },
  406. inputShow: false
  407. },
  408. {
  409. style: {
  410. width: '114px',
  411. height: '48px',
  412. display: 'flex',
  413. justifyContent: 'flex-start',
  414. alignItems: 'center',
  415. paddingLeft: '10px',
  416. boxSizing: 'border-box'
  417. },
  418. childrenStyle: {
  419. display: 'block',
  420. width: "94px",
  421. height: '52px',
  422. background: 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKMAAADlBAMAAADNQ1rCAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURUdwTOZDR////988QPna2ul9f/Gsrf7w8O2IieZSVfbGx+hkZ/Gen6e7LO8AAAABdFJOUwBA5thmAAAIX0lEQVR42u2azVPbSBbA509QI3Bh4DIvsqQ4vojmYxlyiVBgyeSCjQKEuSASM0m4RGNwSOaCWQdtZS4skO8LIjMVvHshO6SS+IQnX1OTP2pfSzJ4CksxZqs2W9WvCrktlX966n4tlX70V+R48VUDwZEcyZEcyZHNIGGGbTueE9IiY+NBkpDC6RMhRR+ZRlw3Q7qSMWrnjAvNIjsdx4HnjrNM3B/GDV0xjA4AkNhfs8gY+PF1J5gFr1WRXyRWU/0y1U6IPNWa7E+a6aQ52Z8bMQz8yzSLpBgwi5uK7N6h3TK29JyBAZlmL1wMkFoFYJ715UWqD2cxYLlZ5F32a8bYmJP6WA/I1SxXmkVaQV8CtZYGi6Zp3qP6z/hhNo0U32I+EsvqLFydNovF4pSm++doOktK+2C7FzszDTDMSOqJs8ShltbTSUr7AbJgGLoqnjRL8SzcoT2wTc+amC8mq1LTSpiXwFxveo5XlMyAY8tkcPL6sIc8GLBmkW3B7/eRJHsX/tYArCJsNjt7RNtHqgVQguEZhxXaiwk3jcxmi+YUXZ29P419aZqWSudwVp4E6U1ySgfYZtCkdHOKDmbwOzZPMse9qDY0jeUn0hPc1T0KoZrIOIxL2BWLNZd9oseZyJ/jHMmRHPl/jeTB48RBThgCR3IkR3IkR/53kL7NIiSmB425iyRamX0O6Quyh45zF245jkNI3F4i0cpMaEiQFaqv4eOG9/ZsXIxQZkJDguwAWW3InWBWQpRZQ8hT1ddT+jJry8w9fd9zRJk1l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fontSize: '12px'
  425. },
  426. inputShow: false,
  427. button: true,
  428. },
  429. ],
  430. allPrice: 0.00,
  431. checked: true
  432. }
  433. },
  434. methods: {
  435. reduce (list) {
  436. let obj = this.contentList[list.id - 1];
  437. if (obj.count <= 0) {
  438. } else {
  439. obj.count--
  440. obj.totalCount = changeTwoDecimal_f(parseFloat(obj.price) * parseInt(obj.count))
  441. Vue.set(this.contentList, list.id - 1, obj)
  442. }
  443. },
  444. add (list) {
  445. console.log(list)
  446. let obj = this.contentList[list.id - 1];
  447. obj.count++
  448. obj.totalCount = changeTwoDecimal_f(parseFloat(obj.price) * parseInt(obj.count))
  449. Vue.set(this.contentList, list.id - 1, obj)
  450. },
  451. handle(list) {
  452. let obj = this.contentList[list.id - 1];
  453. obj.checked = false
  454. Vue.set(this.contentList, list.id - 1, obj)
  455. }
  456. },
  457. components: {
  458. 'cart-header': {
  459. props: ["list"],
  460. data() {
  461. return {
  462. checked: false
  463. }
  464. },
  465. methods: {
  466. },
  467. template: `
  468. <div class="header">
  469. <div class="layout">
  470. <ul>
  471. <li v-for="item in list" :key="item.id" :style="item.style">
  472. <input type="checkbox"
  473. v-if="item.inputShow"
  474. :checked="item.checked">
  475. <span :style="item.childrenStyle">
  476. {{ item.text }}
  477. </span>
  478. </li>
  479. </ul>
  480. </div>
  481. </div>`,
  482. },
  483. 'cart-footer': {
  484. props: ["list", "allPrice"],
  485. data() {
  486. return {
  487. checked: false
  488. }
  489. },
  490. computed: {
  491. price () {
  492. for (const key in this.$attrs) {
  493. return parseFloat(this.$attrs[key])
  494. }
  495. }
  496. },
  497. methods: {
  498. changeTwoDecimal_f (x) {
  499. var f_x = parseFloat(x);
  500. if (isNaN(f_x)) {
  501. return 0;
  502. }
  503. var f_x = Math.round(x * 100) / 100;
  504. var s_x = f_x.toString();
  505. var pos_decimal = s_x.indexOf('.');
  506. if (pos_decimal < 0) {
  507. pos_decimal = s_x.length;
  508. s_x += '.';
  509. }
  510. while (s_x.length <= pos_decimal + 2) {
  511. s_x += '0';
  512. }
  513. return s_x;
  514. }
  515. },
  516. template: `
  517. <div class="footer">
  518. <div class="layout">
  519. <ul>
  520. <li v-for="item in list" :key="item.id" :style="item.style">
  521. <input type="checkbox" v-if="item.inputShow" :checked="item.checked" @click="handle()">
  522. <span :style="item.childrenStyle" v-if="!item.button">
  523. {{ item.text }}
  524. </span>
  525. <span v-if="item.priceShow" style="color: rgb(230,67,71)">¥
  526. {{ changeTwoDecimal_f(price) }}</span>
  527. <span v-if="item.button" :style="item.childrenStyle"></span>
  528. </li>
  529. </ul>
  530. </div>
  531. </div>`
  532. },
  533. 'cart-main': {
  534. props: ['list'],
  535. data() {
  536. return {
  537. mixinsDisable: false,
  538. addDisable: false,
  539. }
  540. },
  541. methods: {
  542. reduce (list) {
  543. if (parseInt(list.count) < 1) {
  544. this.mixinsDisable = true
  545. } else {
  546. this.$emit("reduce", list)
  547. }
  548. },
  549. add (list) {
  550. if (list.count >= 0) {
  551. this.mixinsDisable = false
  552. }
  553. this.$emit("add", list)
  554. },
  555. handle (list) {
  556. this.$emit("handle", list)
  557. }
  558. },
  559. template: `
  560. <main>
  561. <input type="checkbox" v-if="list.inputShow" :checked="list.checked" @click="handle(list)">
  562. <img :src="list.imgUrl" alt="">
  563. <div class="content">{{ list.content }}</div>
  564. <div class="price">¥{{ list.price }}</div>
  565. <div class="count">
  566. <div class="countNum">
  567. <input type="button" value="-" :disabled="mixinsDisable" @click="reduce(list)">
  568. <input type="text" disabled maxlength="3" :value="list.count">
  569. <input type="button" value="+" :disabled="addDisable" @click="add(list)">
  570. </div>
  571. <span class="stock" v-if="list.stock"
  572. style="color: red; font-size: 10px;font-weight: 300;">无货</span>
  573. </div>
  574. <div class="total">¥{{ list.totalCount }}</div>
  575. <div class="edit">
  576. <div v-for="item in list.edit" :key="item.id">
  577. <a href="" v-if="item.type == 1">{{ item.text }}</a>
  578. <a href="" v-else-if="item.show && item.type == 2">{{ item.text }}</a>
  579. <a href="" v-else-if="item.type == 3">{{ item.text }}</a>
  580. </div>
  581. </div>
  582. </main>`,
  583. }
  584. }
  585. })
  586. </script>
  587. </body>
  588. </html>