2.组件.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. </head>
  8. <body>
  9. <div id="app">
  10. <chenyou></chenyou>
  11. <my-com></my-com>
  12. {{show}}
  13. </div>
  14. <script src="./vue.js"></script>
  15. <script>
  16. // 全局注册组件
  17. Vue.component('my-com',{
  18. template:`
  19. <div>1111</div>
  20. `
  21. })
  22. // 定义组件 Vue.extend
  23. const zhenBo = Vue.extend({
  24. // name 组件名
  25. name:"zhenBo",
  26. // template 组件模板
  27. template:`
  28. <div>
  29. <h2>马琛笑得真好看</h2>
  30. <p>
  31. {{msg}}
  32. </p>
  33. </div>
  34. `,
  35. // 组件中数据
  36. data() {
  37. return {
  38. msg:"嘿嘿嘿"
  39. }
  40. }
  41. })
  42. // vue实例
  43. new Vue({
  44. el:"#app",
  45. data:{
  46. show:"卢勃玮也想来"
  47. },
  48. // 注册组件
  49. components:{
  50. // 标签名要小写
  51. chenyou: zhenBo
  52. }
  53. })
  54. </script>
  55. </body>
  56. </html>