4.VueComponent.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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>VueComponent</title>
  7. <!-- 引入vue -->
  8. <script type="text/javascript" src="../js/vue.js"></script>
  9. </head>
  10. <body>
  11. <!--
  12. 关于VueComponent:
  13. 1、school组件本质时一个名为VueComponent的构造函数,且不是程序员定义的,
  14. 是Vue.extend生成的。
  15. 2、我们只需要写<school/>或<school></school>,Vue解析时会帮我们创建school组件
  16. 的实例对象。
  17. 3、特别注意:每次调用Vue.extend,返回的都是一个全新的VueComponent!!!
  18. 4、关于this指向:
  19. (1)、组件配置中:
  20. data函数、methods中的函数、watch中的函数、computed中的函数,他们的this
  21. 均是【VueComponent实例对象】
  22. (2)、new Vue(options)配置中:
  23. data函数、methods中的函数、watch中的函数、computed中的函数,他们的this
  24. 均是【Vue实例对象】
  25. 5、VueComponent的实例对象,以后简称vc(也可称之为:组件实例对象)。
  26. Vue的实例对象,以后简称vm。
  27. -->
  28. <!-- 准备好一个容器 -->
  29. <div id="root">
  30. <school></school>
  31. <hello></hello>
  32. </div>
  33. </body>
  34. <script type="text/javascript">
  35. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示
  36. // 定义school组件
  37. const school = {
  38. name:'atguigu',
  39. template:`
  40. <div>
  41. <h2>学校名称:{{name}}</h2>
  42. <h2>学校地址:{{address}}</h2>
  43. <button @click="showName">点我提示学校名</button>
  44. </div>
  45. `,
  46. data() {
  47. return {
  48. name: '尚硅谷',
  49. address: '北京'
  50. }
  51. },
  52. methods: {
  53. showName(){
  54. alert(this.name)
  55. }
  56. },
  57. }
  58. const test = Vue.extend({
  59. template:`<span>atguigu</span>`
  60. })
  61. // 定义hello组件
  62. const hello = Vue.extend({
  63. template:`
  64. <div>
  65. <h2>{{msg}}</h2>
  66. <test></test>
  67. </div>
  68. `,
  69. data(){
  70. return{
  71. msg:'你好'
  72. }
  73. },
  74. components:{test}
  75. })
  76. console.log('@',school)
  77. console.log('#',hello)
  78. // 创建vm
  79. new Vue({
  80. el:'#root',
  81. components:{school,hello}
  82. })
  83. </script>
  84. </html>