21_components_组件传参.html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./js/vue.js"></script>
  8. <style>
  9. .box {
  10. width: 100px;
  11. height: 100px;
  12. background-color: red;
  13. margin: 10px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="app">
  19. <box val="helloworld"></box>
  20. <box v-bind:val="str"></box>
  21. </div>
  22. <template id="box-temp">
  23. <div class="container">
  24. <div @click="box1Handle" class="box">
  25. <span>{{val}}</span>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. let app = new Vue({
  31. el: '#app',
  32. data: {
  33. str:"lovecoding"
  34. },
  35. components: {
  36. box: {
  37. template: "#box-temp",
  38. // 组件内部的data是个函数 内部返回一个对象
  39. data() {
  40. return{
  41. num:"abcd"
  42. }
  43. },
  44. // props用来接受传递进来的参数
  45. props:["val"],
  46. methods: {
  47. box1Handle() {
  48. console.log(this.val)
  49. }
  50. }
  51. }
  52. }
  53. })
  54. </script>
  55. </body>
  56. </html>